Asked  7 Months ago    Answers:  5   Viewed   40 times

I have a mysql table. column like that

series_id, series_color, product_name

I should list same series_id product like with group.

I want like that list all same saries_id echo my screen like that

A12 Series Product

 - Milk  
 - Tea 
 - sugar
 - water

B12 Series Product

 - Water 
 - Banana 
 - Cofee 
 - Tea

 Answers

10

Order your results by series_id, so all the products with the same value will be together.

$stmt = $pdo->prepare("SELECT series_id, product_name
                       FROM yourTable
                       ORDER BY series_id");
$stmt->execute();

Then when displaying the results, show the Series header and start a new <ul> whenever it changes:

$last_series = null;
echo "<ul>n";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    if ($row['series_id'] != $last_series) {
        if ($last_series) {
            echo "</ul></li>n";
        }
        echo "<li>" . $row['series_id'] . " Series Productn";
        echo "<ul>n";
        $last_series = $row['series_id'];
    }
    echo "<li>" . $row['product_name'] . "</li>n";
}
if ($last_series) {
    echo "</li>n";
}
echo "</ul>n";
Tuesday, June 1, 2021
 
Kemrop
answered 7 Months ago
81

You are missing the closing " at option="value <-- in your HTML

Note that

$row = $result->fetch_array()

can be replaced by

$row = $result->fetch_assoc()

Doing so, the array for each record you fetch would take half of the size.

Wednesday, March 31, 2021
 
BrunoRamalho
answered 9 Months ago
75

One query is fine. As you have it, and probably the better opton. You have to work out which is more efficient, to let MySQL take the strain, or the network and PHP take the strain. It's a lot better to let PHP take the strain than MySQL, but where MySQL has "inbuilt" features, such as the grouping you desire, then leave the MySQL and save the network traffic.

To make this work: add "ORDER BY p.post_id, pc.comment_id" to your query - this gets the results in order.

Then, if you must build into an array (although you may be able to process directly without using an array, the method would be similar):

$lastPostID = 0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    if ($lastPostID <> $row['post_id']) {
        $lastPostID  = $row['post_id'];
        $answers[$lastPostID] = array('post_id' => $row['post_id'],
                                      'author_id' => $row['author_id'],
                                      etc
                                      'comments' => array() );
    }
    $answers[$lastPostID]['comments'][] = array('comment_id' => $row['comment_id'], 'coment' => $row['comment'] etc);
}
Saturday, May 29, 2021
 
PHLAK
answered 7 Months ago
83

This is happening because when encrypting, the data needs to be a certain size. Your data is smaller than that, so it's being padded. PHP pads data with NULL characters, so when you rtrim, you're removing both your NULLs and the ones added with PHP.

One solution is to pad the data to the correct length by using another method. I personally use PKCS7 padding.

Here's an example of how to use this padding method (adapted from https://gist.github.com/1077723):

$data = "Test Datax00x00";

$key = "mykey";

$blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);

// PKCS7 Padding
$pad = $blocksize - (strlen($data) % $blocksize);
$data .= str_repeat(chr($pad), $pad);

$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, MD5($key, true), $data, MCRYPT_MODE_ECB);

$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, MD5($key, true), $encrypted, MCRYPT_MODE_ECB);

// PKCS7 Padding
$strPad = ord($decrypted[strlen($decrypted)-1]);
$newData = substr($decrypted, 0, -$strPad);

DEMO: http://ideone.com/bMZxyf

Saturday, May 29, 2021
 
PHLAK
answered 7 Months ago
54

Using array_search(), and array_column()

<?php

$list_team = array( 
                (object)array(
                    'id' => 1, 
                    'name' => 'chelsea.jpg'
                ),
                (object)array(
                    'id' => 2, 
                    'name' => 'mu.jpg'
                ),
                (object)array(
                    'id' => 3, 
                    'name' => 'arsenal.jpg'
                ),
            );

    $team = 'chelsea.jpg';

    // array column, returns all value of sub array, with key name
    // array_search will return key
    $key = array_search($team, array_column($list_team, 'name'));

    if($key!==false){

        // your object will be
        print_r($list_team[$key]);

        // access remaining..
        echo $list_team[$key]->name.' '. $list_team[$key]->id.PHP_EOL;
    }
?>
Saturday, May 29, 2021
 
Grzegorz
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share