Find array that matchs from database; and output value from column next to it

Hello, so I got a json from server:

I build a variable with the json answers with the data.


If I output my projects $myitemsname I get codes insted of the names.


So I have made a table in mysql with the codes and the names since I named them I know what the codes translate to.

ID | itemcode | itemname
1  | wqerqwet | Silver Chairs
2  | qweqwet  | Black Metal Chairs
3  | qwezxxz  | White Chairs

I output all the data from the json in a foreach loop to an html table:

item | stock | whatever
wqerqwet | 2 | etc
qweqwet | 1 | etc
qweqwet | 5 | etc

The problem I have is I don’t know how to use the code to search the mysql database I made, and search the value in the column next to it(the item name)and replace this value in the html table for the code.

I’ve been trying to test and make a code with one Item to test it but still haven’t got the result I want, I’m kinda of lost.

  $item = "qwetqwe";
  require_once 'connection.php';
  $stmt = $pdo->prepare("SELECT itemcode FROM codestable WHERE $item=?");
  $result = $stmt->fetch();
    if(array_search($item, $result))
    echo "Match found";
  } else {
    echo "Not found";

You would execute one query that gets the itemnames corresponding to the $myitemnames values, at one time, index/pivot the data using the itemcode when you fetch it, then as you are looping to produce the output, test and get the current itemname.

$sql = "SELECT itemcode, itemname FROM codestable WHERE FIND_IN_SET(itemcode,?)";
$stmt = $pdo->prepare($sql);
// implode the input itemcode values, making it a comma separated list for the FIND_IN_SET 2nd parameter
$stmt->execute([ implode(',',$myitemnames) ]);

// fetch the data as a array, indexing it by the first column selected - itemcode
$codes = $stmt->fetchAll(PDO::FETCH_UNIQUE);

// you can print_r($codes) to see what the result looks like

// as you are looping to produce the output, assuming $item is the current itemcode value

$itemname = isset($codes[$item]) ? $codes[$item] : 'No item name found';
1 Like

Thanks for your answer, I learned a lot of stuff from it worked well.

But how do I replace the new array with the old one in my foreach loop?

I’m looping straight out of the json

foreach ($whatever as $index=> $what): ;

echo "<tr><td>$what</td><td>$myitemsname[$index]</td><td>$myitemsquantity[$index]</td></tr>"


If I do a for each loop with the solution you gave me I get to separate the codes and the names. But I’m not seen how to make a replace in the html made by the first foreachloop.

whatever |    item | stock 
  etc |   wqerqwet | 2
 etc |  qweqwet | 1 
 etc | qweqwet | 5  

I’ve red a lot today but maybe I’m to burn out. Maybe I should use another type of loop?

Inside your foreach loop code, $myitemsname[$index] is the $item value that you would use to get the $itemname out of the $codes array.

1 Like

Your unreal(I mean this in a good way, english not my main language), I hope I learn more and more and get close to what you know. Your just looking at a part of a code, but nothing escapes you, I really mean when I say I wanna learn more like you do. Thanks a lot.

Sponsor our Newsletter | Privacy Policy | Terms of Service