Searching in multidimensional array using PHP [duplicate]

I have an array like this:

$programs=[
    'user1'=>[
        'value 1',
        'value 2',
    ],
    'user2'=>[
        'value 3',
    ],
    'user3'=>[
        'value 4',
    ],
];

I would like to get the “user” corresponding with the value without having to loop through the array every time.

The following only works when having keys in the nested array:

$key=array_search('value 3', array_column($programs, 0));

Docker and PostgreSQL problem – Drop database

I have a problem with Docker, or more precisely with the PostgreSQL database to which an application from another container connects.
For some time now, the database has been randomly dropped and I have to recover the database from a copy. Where to look for the problem> I am attaching screenshots.




PHP map one array to another array [closed]

I have an array with the following structure:

myArray => array (
    'id' => array (
        0 => 'ID1',
        1 => 'ID2',
    ),
    'a' => array (
        0 => 'A1',
        1 => 'A2',
    ),
    'b' => array (
        0 => 'B1',
        1 => 'B2',
    ),
    'c' => array (
        0 => 'C1',
        1 => 'C2',
    ),
    'd' => array (
        0 => '2000',
        1 => '3000',
    )
)

But, I need to remap it to have the following structure:

myNewArray => array (
    0 => array (
        'id' => 'ID1',
        'a' => 'A1',
        'b' => 'B1',
        'c' => 'C1',
        'd' => 2000
    ),
    1 => array (
        'id' => 'ID2',
        'a' => 'A2',
        'b' => 'B2',
        'c' => 'C2',
        'd' => 3000
    )
)

Even though I only placed two indices, the idea is to go through the entire array (which can have much more than 2 indices) and remap it.

How to do this? Thanks!

Class “MongoDBDriverManager” not found

My PHP version is 8.2.4
My Laravel version is 10.10
i am using xampp.
the issue is because of the php extention of mongodb in my xampp.
i can not be able to have any extensions for mongodb.
can anyone help me to solve this issue.

I have tried coping mongodb.dll file but still its not working

parent without constructor in this concrete case is correct PHP8?

is this code correct not in terms of functionality, because works. In terms of encapsulation etcetera? IMO is weird parent doesn’t have a constructor. But I think parent doesn’t need a constructor in this case? What do you think? Thanks.


class DTO extends ParentDTO
{
    public function __construct(
        protected ?bool $test,
        protected string $message,
    ) {}

    public function test(): ?bool
    {
        return $this->test;
    }
}

abstract class ParentDTO
{
    protected string $message;

    public function message(): ?bool
    {
        return $this->message;
    }
}

I could not pass php row id through modal (BS v5.3)

I would like to pass php row id from while loop through modal in order to create edit modal form.

Here is my modal toggle

<a data-bs-toggle="modal" data-bs-target="#modaledit" 
data-bs-whatever="?id=<?=$row['staff_id'];?>">
<i class="fa-solid fa-pencil fa-lg" style="color: #fec700;"></i></a>

As you can see, I would like value in

data-bs-whatever="?id=<?=$row['staff_id'];?>"

which is my row id to go in sql and search data on that row ($coverid = row id) by

if (isset($coverid)) {
    $sqlu = " select * from tb_staff";
    $sqlu .= " where";
    $sqlu .= " staff_id='$coverid'";
    $resultu = $cls_conn->select_base($sqlu);
    while ($rowu = mysqli_fetch_array($resultu)) {
        $staff_name = $rowu['staff_name'];
        $staff_lastname = $rowu['staff_lastname'];
    }
}
?>

then my edit modal could show the right value in that row

<div class="modal fade" id="modaledit" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog model-xl">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title fs-5" id="staticBackdropLabel">Edit Form</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form id="modal_edit" method="POST">

                       <div class="row g-2">

                        <div class="col-sm-6">
                            <div class="form-floating">
                                <input id="staff_name" name="staff_name" type="text" class="form-control" placeholder="placeholder" required="required" value="<?= $staff_name; ?>">
                                <label for="staff_name">Name</label>
                            </div>
                        </div>

                        <div class="col-sm-6">
                            <div class="form-floating">
                                <input id="staff_lastname" name="staff_lastname" type="text" class="form-control" placeholder="placeholder" required="required" value="<?= $staff_lastname; ?>">
                                <label for="staff_lastname">Lastname</label>
                            </div>
                        </div>

                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                <button type="submit" name="editmodal" class="btn btn-warning" form="modal_edit">Edit</button>
            </div>
        </div>
    </div>
</div>

Edited : My unworked Javascript

const modaledit = document.getElementById('modaledit')
if (modaledit) {
  modaledit.addEventListener('show.bs.modal', event => {
  
    const button = event.relatedTarget
 
    var coverid = button.getAttribute('data-bs-whatever')

I think my Javascript is my issue, how can i fix it?

I just want my edit-modal able to show data from the right row.

PHP mysqli query not working from php file, but working from command line [closed]

I am trying to query a database from php:

<?php
$conn = new mysqli('localhost', 'root', 'password!', 'db');
$item = $_POST['item'];
$username =$_POST['username'];

$conn->query("DELETE FROM userData WHERE username = '$username' AND listdata = '$item';")

even though the $username and $item match the values in a row in the database, this query fails to delete that row.

If you remove the AND listdata = '$item' portion of the query it does delete rows. Does anyone know why the one part of the query makes it not work? Thanks.

Woocommerce – Display variation price in the add to cart button

I am trying to add the variation price in the add to card button in a wordpress website with woocommerce.
I tried this solution but:

  • when a variation is selected it returns the price 2 times
  • if selecting an other variation, it doesn’t change the price, but add a new one behind it
  • it doesn’t remove the price when the selection is cleared.

Any idea of how to make it work?

I am expecting:

  • The price to display in the button when a variation is selected
  • The price to change when there is a change in the variation selection
  • The price to disapear when clearing the selection

Prevent HTTPS Redirection on a single PHP File using .htaccess in WordPress

The website URL in WordPress is http://example.com. The website is auto-redirected to https://example.com. I need to prevent HTTPS redirection of http://example.com/akvalidate.php only

I have added the following to .htaccess but it does not work. It keeps fetching the https version.The website is having LiteSpeed cache turned on.

RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^akvalidate.php$ http://example.com/akvalidate.php? [R=301,L]

How to use Direction API [closed]

I want full code php and html when we give longtitude and latitude , when click naviagte button and show map and show live location with another direction.

I want when we click navigate button and also open map app show direction with two adress.

Javascript Not Loaded In Livewire

So i tried to use Sweetalert to show delete confirmation modal and I’m using Livewire powergrid to show the list of my data.
The problem is. If I’m navigating to the List components. The Javascript, that i pushed in the list components are not working.
But if i refresh the page in the list componets. It works again.

I’ve tried to use wire:navigated on the javascript add event listeners. It’s still not working
I’ve tried to add data-navigate-once in my script tag. It’s still not working.

So, down here is how it works.
The data generated using powergrid in the data I’m adding row action button

<?php

#[On('client-deleted')]
    public function clientDeleted($clientId) {}

    public function actions(AppModelsClient $row): array
    {
        return [
            // Edit Button

            Button::add('delete')
                ->slot('Delete')
                ->id()
                ->class('btn btn-danger')
                ->dispatchTo('clients.list-client', 'trigger-delete-client', ['client' => $row])
        ];
    }

If we click the button it will trigger an event in my list-client component, as You can see the script section.

<div>
    <x-page-header pageName="Client" />

    <x-partials.flash />

    <div class="row">
        <div class="col-12">
            <div class="card shadow-lg mb-4">
                <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                    <h4 class="m-0 font-weight-bold text-primary">List Client</h4>
                    <a href={{ route('client.create') }} class="btn btn-primary" wire:navigate>
                        <i class="bi bi-person-plus-fill"></i>
                        <span>Client</span>
                    </a>
                </div>
                <div class="card-body">
                    <livewire:tables.client-table />
                </div>
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script type="text/javascript" data-navigate-once>
    document.addEventListener('livewire:navigated', function () {

        @this.on('trigger-delete-client', client => {
            Swal.fire({
                title: 'Are You Sure?',
                text: 'Conatct record will be deleted!',
                icon: "warning",
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#aaa',
                confirmButtonText: 'Delete!'
            }).then((result) => {
         //if user clicks on delete
                if (result.value) {
             // calling destroy method to delete
                    @this.call('destroy', client)
             // success response
                    Swal.fire({title: 'Contact deleted successfully!', icon: 'success'});
                } else {
                    Swal.fire({
                        title: 'Operation Cancelled!',
                        icon: 'success'
                    });
                }
            });
        });
    })
</script>
@endpush

And it will show a modal confirmation box. If we click Yes/Confirm it will run this code. which will delete the data and reload it. For the reload part, you can scroll back to the Table component above where I’m defining it right before i create a delete button.

<?php
class ListClient extends Component
{
    public function render()
    {
        return view('livewire.clients.list-client');
    }

    public function destroy(AppModelsClient $client)
    {
        try {
            DB::beginTransaction();

            $client->delete();

            DB::commit();
        } catch (Throwable $th) {
            DB::rollBack();
            Log::error($th);
        }

        $this->dispatch('client-deleted', $client->id)->to(ClientTable::class);
    }
}

I’ve followed this tutorial, but maybe because my lack of knowledge, It’s not working.

Why every time I run PHP Artisan Serve it appears like the dashboard that I gor this from where?

This is my first time use laravel blade and composer, i am so confuse after i run php artisan serve When I enter the http://localhost:8000/news that should appear the display of the code I wrote in news.blade.php but when I run it out as shown in this below
enter image description here

Anyone can explain me what is this and why this is appear after i run php artisan serve.
this is what CMD looks like after running php artisan serve.
enter image description here

How to downgrade the PHP version 7.2?

I have the PHP and Perl code hosted on Bluehost. It worked well till 3 months ago, but some features don’t work recently. I think it is the PHP version problem. I have checked the Bluehost, there is an option to upgrade the PHP version to 8.1, 8.2, no option to downgrade to PHP 7.2. Any suggestions?

I checked the code and made some changes to get it worked as expected. I expect to get it working well as before.

Graphql library helper to connect to external endpoint

For a project I have to do some requests in PHP to a GraphQL endpoint. I’m familiar with Graphql to build query’s. The most of the project is build in node.js, where I use axios to handle the requests.

But in PHP I couldn’t figure it out, it seems I’m having problems setting up the request (correct URL construction, sending headers, ..).

I’m looking for a library / helper that can connect an external GraphQL Server or an example how to build this in PHP properly.

Most of the thing on the internet I found is to create an own graphql server, but this is not what I want. I really need to contact the external server.

Things I tried/found:
Library Graphql-php: https://github.com/webonyx/graphql-php (didn’t found how to use it for an external server)
File_get_context

Some that can help me?