2

How else can I get the name without putting the query in the foreach loop?

PHP:

$material_decode=json_decode($materials['materials']);
foreach($material_decode as $material_id=>$material_value)
{
    $forge_materials=mysql_fetch_assoc(mysql_query('SELECT `name` FROM `forging_materials` WHERE `id`='.$material_id.' LIMIT 1'));
    echo '<tr>'.
        '<td>'.
            $forge_materials['name'].
        '</td>'.
        '<td>'.
            number_format($material_value).
        '</td>'.
    '</tr>';
}

$material_decode (forge_material_id => material_value):

stdClass Object
(
    [2] => 25
    [4] => 32
    [5] => 23
)

3 Answers 3

2

You can fetch all needed records at once using WHERE IN outside the loop. Only single query will be neccessary this way.

This will work for you:

$material_decode = json_decode($materials['materials'], true);
$forge_materials = mysql_query('SELECT `id`, `name` FROM `forging_materials` WHERE `id` IN ( '. implode(',', array_keys($material_decode)) .')');

while($row = mysql_fetch_assoc($forge_materials))
{
    echo '<tr>'.
      '<td>'.
        $row['name'].
      '</td>'.
      '<td>'.
        number_format($material_decode[$row['id']]).
      '</td>'.
    '</tr>';
}

Keep in mind that mysql_ methods have been deprecated and it is now advised to use PDO or mysqli_ methods instead. You can read more in the docs:

Sign up to request clarification or add additional context in comments.

2 Comments

Dear down-voters - please leave a comment, I'd love to learn what's wrong with that code :)
Thank you very much. I did not think about using IN(). I'm aware mysql_ is depreciated and the site is in the process of getting recoded to use MySQLi.
0

First run SELECT id,name FROM forging_materials WHERE 1 it will fetch all id & names from the table and then in the foreach loop, get the name from the query result which have id equals to $material_id

Comments

0

Updated your code. Try this

$forge_materials_name   =   "";

$material_decode=json_decode($materials['materials']);
foreach($material_decode as $material_id=>$material_value)
{
    if( empty($forge_materials_name) )
    {
        $forge_materials=mysql_fetch_assoc(mysql_query('SELECT `name` FROM `forging_materials` WHERE `id`='.$material_id.' LIMIT 1'));  
        $forge_materials_name   =   $forge_materials['name'];
    }
    echo '<tr>'.
        '<td>'.
            $forge_materials_name.
        '</td>'.
        '<td>'.
            number_format($material_value).
        '</td>'.
    '</tr>';
}

1 Comment

How is this supposed to work? $forge_materials_name will be fetched only for the first material and then used in every loop run. Every row displayed should show the name corresponding to the id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.