Jump to content

$item(1-3) Not displaying defined value


Seancon1

Recommended Posts

<?php

$item1 = "one";
$item2 = "two";
$item3 = "three";
$k = 1;
while($k < 4) {
$prod_item_num = "\$item" . $k;
$unique_query1 = "SELECT * FROM product_db WHERE id = $prod_item_num";
echo $unique_query1;
echo "<br />";
$k++;
}

?>

 

The above displays the following

 

SELECT * FROM product_db WHERE id = $item1
SELECT * FROM product_db WHERE id = $item2
SELECT * FROM product_db WHERE id = $item3

 

I need the $item(1-3) values to echo what I made them. I've tried many different ways, none of which worked.

Link to comment
https://forums.phpfreaks.com/topic/268313-item1-3-not-displaying-defined-value/
Share on other sites

Solved

I found the solution, thank you for looking at my post anyways.

 

<?php

$items = array ( "1" => $item1, "2" => $item2, "3" => $item3 );
$items1 = $items['1'];
echo "The item has been displayed \$items1 = $items1 <br />";
$k = 1;
while($k < 4) {
$prod_item_num = $items["$k"];
$unique_query1 = "SELECT * FROM product_db WHERE id = $prod_item_num";
echo $unique_query1;
echo "<br />";
$k++;
}

?>

 

Displays:

The item has been displayed $items1 = one
SELECT * FROM product_db WHERE id = one
SELECT * FROM product_db WHERE id = two
SELECT * FROM product_db WHERE id = three

 

 

Don't run queries in loops. Get all of the data in one query.

 

$items = array( 'one', 'two', 'three' );
$values = implode( "', '", $items );
$unique_query1 = "SELECT * FROM product_db WHERE id IN ( '$values' )";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.