stew art Posted September 4, 2011 Share Posted September 4, 2011 Hi, I have come up with the following code, I need it to get the details of several scattered products and echo the results, the trick is I don't want it to echo the results one after the other... I want to have the products scattered between unique text on the page but don't want to run the query several times for performance reasons. E.g.- PAGE to look like this: $Product_1 unique text/images $Product_2 $Product_3 unique text/images $Product_4 Current Code: <? $result = mysql_query("SELECT * FROM products where Product_ID IN (475, 465, 234, 567, 845)"); while($row = mysql_fetch_array($result)) { $x = "1"; while ($x<=3) { echo $x; $Product = "Product_"; $Product = $Product.$x; echo $Product; $Product = $row['Product_ID']; echo $Product; $x++; echo $x; } } At the moment it returns the following results: 1 Product_1 465 2 2 Product_2 465 3 3 Product_3 465 4 1 Product_1 475 2 2 Product_2 475 3 3 Product_3 475 4 A few problems... In Blue... it duplicates for product 465 In Red... It repeats again for 475 Also.... it starts with 465, but I want it to go in order as how it appears - $result = mysql_query("SELECT * FROM products where Product_ID IN (475, 465, 234, 567, 845)"); so should start with 475 I want to get the following result: 1 Product_1 475 2 2 Product_2 465 3 3 Product_3 234 4 4 Product_4 567 4 (and so on.....) If anyone could provide me assistance with my troubled 'while loop' statement that would be much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/246398-phpmysql-select-and-display-result-while-array-and-integer-increment/ Share on other sites More sharing options...
Zane Posted September 4, 2011 Share Posted September 4, 2011 I don't see the logic behind having a double while loop. That inner while loop is why it repeats the products. As for getting them in that particular order, that would be better asked in the MySQL board.. which I could move this to if you'd like. Quote Link to comment https://forums.phpfreaks.com/topic/246398-phpmysql-select-and-display-result-while-array-and-integer-increment/#findComment-1265303 Share on other sites More sharing options...
stew art Posted September 4, 2011 Author Share Posted September 4, 2011 Hey Zane, (leave it in PHP for now... the order is only a minor problem). Yes it isn't very logical and I am sure there is a better way! I'm struggling to work out how to get X to increment for each product so I can give each product a unique ID. I originally started with this, which didn't work as it displayed them one after the other - I could not scatter them on the page: while($row = mysql_fetch_array($result)) { echo $row['Product_ID']; echo "<br />"; } It returned: 465 475 234 I progressed to this, which didn't work because X does not increment, and cannot assign a unique reference to each result: while($row = mysql_fetch_array($result)) { $x = "1"; echo $x; $Product = "Product_"; $Product = $Product.$x; echo $Product; $Product = $row['Product_ID']; echo $Product; $x++; echo $x; } Which returned: 1 Product_1 465 2 1 Product_1 475 2 1 Product_1 234 2 Quote Link to comment https://forums.phpfreaks.com/topic/246398-phpmysql-select-and-display-result-while-array-and-integer-increment/#findComment-1265306 Share on other sites More sharing options...
stew art Posted September 4, 2011 Author Share Posted September 4, 2011 Ok, I have worked out, X=1 had to be 'outside' the loop (duh): <? $result = mysql_query("SELECT * FROM products where Product_ID IN (475, 465, 234, 567, 845)"); $x = "1"; while($row = mysql_fetch_array($result)) { $Product = "Product_"; $Product = $Product.$x; echo $Product; $Product = $row['Product_ID']; echo $Product; $x++; } ?> It returns: Product_1 475 Product_2 465 Product_3 234 (and so on..) But, after all that later on in the page I have: <? echo "$Product_1"; ?> Text text text <? echo "$Product_2"; ?> I would expect: 475 Text text text 465 But get: Text text text It's not working... maybe I am doing it wrong, how do I call a specific product from the $result query? e.g. "get and echo Product ID, Product Name, Product Price from $result where ID = 475" Quote Link to comment https://forums.phpfreaks.com/topic/246398-phpmysql-select-and-display-result-while-array-and-integer-increment/#findComment-1265310 Share on other sites More sharing options...
stew art Posted September 4, 2011 Author Share Posted September 4, 2011 Worked the next bit out... $result = mysql_query("SELECT * FROM products where Product_ID IN (475, 465, 234, 567, 845, 22)"); $x = "1"; while($row = mysql_fetch_array($result)) { echo "$x"; echo "<br />"; if ($x==1) $one = $row['Product_ID']; if ($x==2) $two = $row['Product_ID']; if ($x==3) $three = $row['Product_ID']; if ($x==4) $four = $row['Product_ID']; if ($x==5) $five = $row['Product_ID']; $x++; } echo $one; echo "<br />"; echo $two; echo "<br />"; echo $three; echo "<br />"; echo $four; echo "<br />"; echo $five; gave desired result: 1 2 3 4 5 22 234 465 475 845 Now onto the order.... Can this be moved to sql please? I have the statement: SELECT * FROM products where Product_ID IN (475, 465, 234, 567, 845, 22) and when I run this: while($row = mysql_fetch_array($result)) It sorts them then displays them automatically ascending... 22 234 465 475 845 How can I get it to display them in the order I have entered them? e.g.: 475 465 234 567 845 22 Quote Link to comment https://forums.phpfreaks.com/topic/246398-phpmysql-select-and-display-result-while-array-and-integer-increment/#findComment-1265319 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.