Jump to content

PHP/MySQL Select and Display Result While Array AND integer increment


Recommended Posts

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!  :D

 

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.

 

 

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

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"

 

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.