While creating model with factory, migration and resource controller for api inside new repository, conflict encounters in migration as below:

Used Command:

make:model Models/Test/TestApi -a

Error:

root@bb3433d4a92b:/var/www/nt-app-backend# php artisan make:model Models/Test/TestApi -a

Model created successfully.

Factory created successfully.

In ClassLoader.php line 571:

include(/var/www/nt-app-backend/vendor/composer/../../database/migrations/2022_05_06_151610_create_test_apis_table.php): failed to open stream: No such file or directory

How to create stored procedure for loop and queries?

I want to do query optimization. Here is my query with for loop. I need to create Procedure for this query. I am totally confused on how to create procedure.

$interval = $start->diff($end);
$period = $interval->format('%a');
$start_date = $start->format('Y-m-d');
$end_date = $end->format('Y-m-d');

$dsqls="";
for($i = $period; $i >=0;  $i--){
    if ($i == $period){
        $dsqls .= "SELECT  '$start_date' as click_date 
                   union all ";
    }else{
        if($i==0){
            $dsqls .= "SELECT date_sub('$end_date', interval $i day) as click_date ";
        }else{
            $dsqls .= "SELECT date_sub('$end_date', interval $i day) as click_date 
                        union all ";
        }   
    }
}

$sql ="select a.click_date,DATE_FORMAT(a.click_date,'%e') as nameday,
                ifnull(b.count,0) as count
        from ($dsqls) a 
            left join (
                  select date(created_at) as datetime, count(*) as count
                  from enquiries where delete_status='0'
                  group by date(created_at)
                ) b on a.click_date = b.datetime";

Does I want to create procedure for this full query or shall I only create procedure for loop and call procedure here. Is this possible? Pls give me ideas.

Replacing characters in a returned Header Location in PHP

I’m trying to filter returned header location after POST using below PHP code. The returned header location is required for further processing of the payment status and saving the status in the db. The API provider seems not supportive since they don’t reply on time or fail to reply at all.

    $output = curl_exec($curl);
    $lines = explode("n",$output);
    $out = array();
    $headers = true;

    foreach ($lines as $l){
        $l = trim($l);
        if ($headers && !empty($l)){
           if (strpos($l,'location') !== false){
                $p = explode(' ',$l);
                $out['Headers']['location'] = trim($p[1]);
                $url = json_encode($out['Headers']['location']);
                echo json_encode($out['Headers']['location']);
            } 
        } 
    } 

The echo output is as below:- `"https://sandbox.kopokopo.com/api/v1/payments/c122c1d2-8e07-48d3-8c9d-597829447fda"`
How do I make the output to be a valid url without "" ? I'll really appreciate your valuable assistance.

How i can iterate an array multidimensional with depth == n? [duplicate]

i have this array

Array
(
    [Ramo] => Array
        (
            [0] => Array
                (
                    [mq] => 22
                    [path] => /Ramo-0
                    [branch_name] => Ramo-A
                    [contenitore] => Array
                        (
                            [0] => Array
                                (
                                    [path] => /Ramo-0/contenitore-0
                                    [branch_name] => Cont-AA
                                )

                            [1] => Array
                                (
                                    [path] => /Ramo-0/contenitore-1
                                    [asset] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [path] => /Ramo-0/contenitore-1/asset-0
                                                    [branch_name] => Asset-ABA
                                                )

                                        )

                                    [branch_name] => Cont-AB
                                )

                            [2] => Array
                                (
                                    [path] => /Ramo-0/contenitore-2
                                    [branch_name] => cont-AC
                                )

                        )

                ) 
            [2] => Array
                (
                    [mq] => 98
                    [path] => /Ramo-2
                    [branch_name] => RAMO-B
                    [contenitore] => Array
                        (
                            [0] => Array
                                (
                                    [path] => /Ramo-2/contenitore-0
                                    [asset] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [path] => /Ramo-2/contenitore-0/asset-0
                                                    [branch_name] => ASSET-BAA

                                                )

                                            [1] => Array
                                                (
                                                    [path] => /Ramo-2/contenitore-0/asset-1
                                                    [branch_name] => ASSET-BAB

                                                )

                                        )

                                    [branch_name] => CONT-BA
                                )

                            [1] => Array
                                (
                                    [path] => /Ramo-2/contenitore-1
                                    [asset] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [path] => /Ramo-2/contenitore-1/asset-0
                                                    [branch_name] => ASSET-BB

                                                )

                                        )

                                    [branch_name] => CONT-BB
                                )

                        )

                )

            [3] => Array
                (
                    [path] => /Ramo-3
                    [branch_name] => RAMO-C
                )

        )
)

i want a table with this result:

@@@
RAMO A
RAMO B
RAMO C
@@@

@@@
CONT AA RAMO A
CONT AB RAMO A
CONT BA RAMO B
CONT BB RAMO B
@@@

@@@
ASSET ABA CONT AB
ASSET BAA CONT BA
ASSET BAB CONT BA
@@@

How can I go about having that result? The elements within an array could be even more than these.
I’ve been stuck for days and can’t find a solution to that.

delete with eager load in laravel

I’m trying to delete a collection but I want to delete the NFTs related to a collection as well. How do I do this?

‍‍This is my Nft model Nft.php

class Nft extends Model
{
    use HasFactory,SoftDeletes;

    public function collection()
    {
        return $this->belongsTo(Collection::class);
    }
}

This is my collection model Collection.php

class Collection extends Model
{
    use HasFactory, SoftDeletes;
    public function nfts()
    {
        return $this->hasMany(Nft::class);
    }
}

This is my COntroller CollectionController.php

 public function deleteCollection(int $id):RedirectResponse
    {
        Collection::with('nfts')->find($id)->delete();
        return redirect('/collections');
    }

But it did not work!!!
pleas help me!!!!!

How do I pick the Header Location and Discard the rest after POST request in PHP?

Am doing a POST request in PHP and it returns header. Am using the code below.

<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://sandbox.kopokopo.com/api/v1/payments',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => 1,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
  "destination_type": "till",
  "destination_reference": "3eccf0c1-ab6b-41a4-b51a-4e8f588105f1",
  "description": "4563",
  "amount": {
      "currency": "KES",
      "value": "500"
  },
   "metadata": {
      "something": "TST-01",
      "something_else": "Something else"
  },
  "_links": {
      "callback_url": "http://portal.zarafu.com/payments"
  }
 }',
CURLOPT_HTTPHEADER => array(
  'Authorization: Bearer 7NrINmYGFkhzS4sE_6OfuTRivLrbiQluQuTNBpzbMGE',
  'Accept: application/json',
  'Content-Type: application/json'
),
));
$response = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($curl);
//echo $response;
$headers = explode("n", $header);
echo implode($headers);

Am receiving the below header response.

HTTP/1.1 201 Created Content-Type: application/json Transfer-Encoding: chunked Connection: keep-alive Status: 201 Created Cache-Control: no-cache Referrer-Policy: strict-origin-when-cross-origin X-Permitted-Cross-Domain-Policies: none X-XSS-Protection: 1; mode=block X-Request-Id: f2a70f09-9cd5-4b85-b441-e7fa0e84b7e4 location: https://sandbox.kopokopo.com/api/v1/payments/90e02738-0d07-406b-a9c2-cf5867ad5fcf X-Download-Options: noopen X-Runtime: 0.011337 X-Frame-Options: SAMEORIGIN X-Content-Type-Options: nosniff Date: Thu, 05 May 2022 09:57:42 GMT X-Powered-By: Phusion Passenger 6.0.6 Server: nginx/1.18.0 + Phusion Passenger 6.0.6

Am only interested with the line below.

https://sandbox.kopokopo.com/api/v1/payments/90e02738-0d07-406b-a9c2-cf5867ad5fcf

Kindly assist me guys.

Store selected name pulling from db in a dropdown as a different value in db using php and mysql

Below is the code to pull our client names from the db and show it as a dropdown menu:

  <select name='client'>
     <?php
     print_r($client);
     for ($x = 0; $x < count($client); $x++) {
         echo ("<option value=".$client[$x]['clientName']."> ".$client[$x]['clientName']." <br> 
         </option>");
     } 
     ?>
 </select>

After selecting the name and pressing the “submit” button, this value needs to be stored as the associated client ID because that is primary key in our table which is being used as foreign keys in other tables. I’m stuck at this point because I’m not sure how to convert this name to its corresponding ID. This is the code I’ve been trying to update to run this:

      <?php 

      if (isset($_POST['submit'])) {

         $getclientID = "SELECT * FROM ClientAccount WHERE clientName = '{client}' ";
         $Client = getOneRow($getclientID);
         $clientID = $Client['clientID'];
         $clientID = $_POST['clientID'];


         $query = "INSERT INTO ChildInformation (clientID) VALUES ('{$clientID}');";

         runQuery($query);

         echo'<span style="color:red;font-weight:bold;">Successful!</span>';
     };
     ?>

For example, if I select “John Doe” from the dropdown menu and then press “submit” then it should be stored as “1” in the db because that’s John Doe’s client ID.

Thanks for your help!

Duplicate hidden form with not unique input ID

I have a layout that require to have the same form in 2 different place for mobile and desktop view

What I did is to wrap both in a container that hide one or another based on viewport size

I was testing with Google Lighthouse and it complains that form field ID are not unique
Any way to fix this without handle the 2 form field with different ID?

I always add input field with the same ID and NAME

<input type="text" class="form-control" id="name" name="name" value="" required>

I guess I will not have any issue with post variables if I differentiate ID while keeping field name the same

<input type="text" class="form-control" id="name-mobile" name="name" value="" required>

Translate “project” and “project category” slug in Divi theme – wordpress

I managed to modify the standard “project_category” slug into “localita” and the standard “project” slug into “vendita”. This is for the italian side of the website. Now I am translating it into english, and I am trying to get those slug translated as well as follow:

  • localita -> place
  • vendita -> sale
  • vendite -> sales

The php code used so far is the following:

add_filter( 'register_taxonomy_args', 'change_taxonomy_category_project', 10, 2 );

function change_taxonomy_category_project( $args, $taxonomy ) {
    if ( 'project_category' === $taxonomy ) {
        $args['rewrite']['slug'] = 'localita';
    }
    return $args;
}


function change_taxonomy_project() {
    register_post_type( 'project',
        array(
            'labels' => array(
            'name' => __( 'Vendite', 'divi' ),
            'singular_name' => __( 'Vendita', 'divi' ),
        ),
        'has_archive' => true,
        'hierarchical' => true,
        'public' => true,
        'rewrite' => array( 'slug' => 'vendita', 'with_front' => false ),
        'supports' => array(),
    ));
}
add_action( 'init', 'change_taxonomy_project' );

How can I get those string translated editing that code? I am using WPML to translate all the contents.
Currently using WordPress with Divi theme from Elegant Themes

Thanks 🙂

WooCommerce Stock Report

I want to have a report page with all elements currently in stock, a realtime stock status.

This is the code I found and customize

<?php
/*
Template Name: Stock Report :)
*/
if (!is_user_logged_in() || !current_user_can('manage_options')) wp_die('This page is private.');
?>
<!DOCTYPE HTML>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title><?php _e('Stock Report'); ?></title>
    <style>
        body {
            background: white;
            color: black;
            width: 95%;
            margin: 0 auto;
        }

        table {
            border: 1px solid #000;
            width: 100%;
        }

        table td,
        table th {
            border: 1px solid #000;
            padding: 6px;
        }
    </style>
</head>

<body>
    <header>
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                <h1 class="title"><?php the_title(); ?></h1>

                <?php the_content(); ?>

        <?php endwhile;
        endif; ?>
    </header>
    <section>
        <?php

        global $woocommerce;
        ?>
        <table cellspacing="0" cellpadding="2">
            <thead>
                <tr>
                    <th scope="col" style="text-align:left;"><?php _e('Product', 'woothemes'); ?></th>
                    <th scope="col" style="text-align:left;"><?php _e('COD', 'woothemes'); ?></th>
                    <th scope="col" style="text-align:left;"><?php _e('Scorta', 'woothemes'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php

                $args = array(
                    'post_type'            => 'product',
                    'post_status'         => 'publish',
                    'posts_per_page'     => -1,
                    'orderby'            => 'title',
                    'order'                => 'ASC',




                );

                $loop = new WP_Query($args);

                while ($loop->have_posts()) : $loop->the_post();

                    global $product;
                    if ($product->stock > 0) {
                ?>
                        <tr>
                            <td><?php echo $product->get_title(); ?></td>
                            <td><?php echo $product->sku; ?></td>
                            <td><?php echo $product->stock; ?></td>
                            <td><?php echo $product->get_image('thumbnail'); ?></td>

                        </tr>
                <?php
                    }
                endwhile;

                ?>
            </tbody>
        </table>

        <h2>Variations</h2>
        <table cellspacing="0" cellpadding="2">
            <thead>
                <tr>
                    <th scope="col" style="text-align:left;"><?php _e('Variation', 'woothemes'); ?></th>
                    <th scope="col" style="text-align:left;"><?php _e('Parent', 'woothemes'); ?></th>
                    <th scope="col" style="text-align:left;"><?php _e('SKU', 'woothemes'); ?></th>
                    <th scope="col" style="text-align:left;"><?php _e('Stock', 'woothemes'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php

                $args = array(
                    'post_type'            => 'product_variation',
                    'post_status'         => 'publish',
                    'posts_per_page'     => -1,
                    'orderby'            => 'title',
                    'order'                => 'ASC',
                    'meta_query' => array(
                        array(
                            'key'         => '_stock',
                            'value'     => array('', false, null),
                            'compare'     => 'NOT IN'
                        )
                    )
                );

                $loop = new WP_Query($args);

                while ($loop->have_posts()) : $loop->the_post();

                    $product = new WC_Product_Variation($loop->post->ID);
                    $variation = wc_get_product($product);
                    $variation->get_formatted_name();
                    if ($product->stock > 0) {

                ?>
                        <tr>
                            <td><?php echo $product->get_title(); ?></td>
                            <td><?php echo get_the_title($loop->post->post_parent); ?></td>
                            <td><?php echo $product->sku; ?></td>
                            <td><?php echo $product->stock; ?></td>
                            <td><?php echo $product->get_image('thumbnail'); ?></td>
                        </tr>
                <?php
                    }
                endwhile;

                ?>
            </tbody>
        </table>
</body>

</html>

the first loop has the simple product, the second loop the variable product.
Can you help me to have an unique loop?

So how can I group the variable product and have the total stock?
thanks

How the WP-Automatic plugin works even it is not running on the cloud

There is a plugin for wordpress, WP-Automatic which fetches the RSS feed of other websites and create a post on our website, but how this works automatically. We are not running the plugin on cloud but it fetches the post from other website and add it. If we want to execute a PHP script the page must remain open or we may send a request to that page but there is nothing happening like that in this plugin.

Regex replace between html tags not working well

I want to use this function to replace text between HTML tags:

<?php
function Obfuscate($html)
{
    $html = str_replace(array(
        "r",
        "n",
        "   "
    ) , '', $html);
    $html = preg_replace_callback_array([
    '/<(a|b|button|center|div|em|fieldset|font|FONT|SPAN|form|h1|h2|h3|h4|h5|h6|i|noscript|ol|optgroup|option|p|small|span|strong|sub|sup|td|textarea|th)([^<]*)>([^<>&]+)<([/]?)\1/' => function ($m)
    {
        $r = Replace($m[3], "yes");
        return "<" . $m[1] . $m[2] . ">" . $r . "<" . $m[4] . $m[1];
    },
    '/<(a|b|button|center|div|em|fieldset|font|FONT|SPAN|form|h1|h2|h3|h4|h5|h6|i|noscript|ol|optgroup|option|p|small|span|strong|sub|sup|td|textarea|th)([^<]*)>([^<>&]+)<([/]?)\1/' => function ($m)
    {
        $r = Replace($m[3], "yes");
        return "<" . $m[1] . $m[2] . ">" . $r . "<" . $m[4] . $m[1];
    }
    ], $html);
    return $html;
}


function Replace($pain_text = "", $what)
{
    $crypt = array(
        "A" => "065",
        "a" => "097",
        "B" => "066",
        "b" => "098",
        "C" => "067",
        "c" => "099",
        "D" => "068",
        "d" => "100",
        "E" => "069",
        "e" => "101",
        "F" => "070",
        "f" => "102",
        "G" => "071",
        "g" => "103",
        "H" => "072",
        "h" => "104",
        "I" => "073",
        "i" => "105",
        "J" => "074",
        "j" => "106",
        "K" => "075",
        "k" => "107",
        "L" => "076",
        "l" => "108",
        "M" => "077",
        "m" => "109",
        "N" => "078",
        "n" => "110",
        "O" => "079",
        "o" => "111",
        "P" => "080",
        "p" => "112",
        "Q" => "081",
        "q" => "113",
        "R" => "082",
        "r" => "114",
        "S" => "083",
        "s" => "115",
        "T" => "084",
        "t" => "116",
        "U" => "085",
        "u" => "117",
        "V" => "086",
        "v" => "118",
        "W" => "087",
        "w" => "119",
        "X" => "088",
        "x" => "120",
        "Y" => "089",
        "y" => "121",
        "Z" => "090",
        "z" => "122",
        "0" => "048",
        "1" => "049",
        "2" => "050",
        "3" => "051",
        "4" => "052",
        "5" => "053",
        "6" => "054",
        "7" => "055",
        "8" => "056",
        "9" => "057",
        "&" => "038",
        " " => "032",
        "_" => "095",
        "-" => "045",
        "@" => "064",
        "." => "046"
    );
    $r = "";
    for ($i = 0;$i < strlen($pain_text);$i++)
    {
        $y = substr($pain_text, $i, 1);
        if (array_key_exists($y, $crypt))
        {
            $rand1 = rand(1, 3);
            if ($what == 'yes')
            {
                $r = $r . "<span style='font-size:0px;'>&#9760;" . rand(1, 10) . "</span>" . "&#" . $crypt[$y] . ";" . "<span style='font-size:0px;'>" . rand(1, 10) . "</span>";
            }
            else
            {
                $r = $r . "&#" . $crypt[$y] . ";";
            }
        }
        else
        {
            $r = $r . $y;
        }
    }
    if ($what == 'yes')
    {
        $r = $r . "<span style='font-size:0px;'>&#9760;</span>";
        return $r;
    }
    else
    {
        return $r;
    }
}
?>

This function works well obfuscating HTML code it takes text between HTML and encodes it. The problem is when I have an HTML code when we have two HTML tags inside each other like this examples

<p><b>TEXT 1</b> TEXT 2</p>

What it does is that it encodes only what is between the inner HTML tag and avoids the other text.

So TEXT 1 is encoded but TEXT 2 is ignored. I hope you can find me a solution.

PHP : How to select specific parts of a string

I was wondering… I have two strings :

"CN=CMPPDepartemental_Direction,OU=1 - Groupes de sécurité,OU=CMPP_Departementale,OU=Pole_Ambulatoire,OU=Utilisateurs_ADEI,DC=doadei,DC=wan",
"CN=CMPPDepartemental_Secretariat,OU=1 - Groupes de sécurité,OU=CMPP_Departementale,OU=Pole_Ambulatoire,OU=Utilisateurs_ADEI,DC=doadei,DC=wan"

Is there a way in php to select only the first part of these strings ? I would like to just select CMPPDepartemental_Direction and CMPPDepartemental_Secretariat.

I had thought of trying with substr() or trim() but without success.