Jump to content

Recommended Posts

id

name

1

Adam

2

Bill

 

<?php

$id_list_array = array(1,2);

			foreach ($id_list_array as $id_number) {
			echo "$id_number<br />";
				if ($row['id'] == $id_number)
				{
					echo $row['id'] . "<!!!>" . $row['name'] . "<!!!>added\n";
					break;
				} else {
					echo $row['id'] . "<!!!>" . $row['name'] . "<!!!>notadded\n";
					break;
				}
			}
?>

 

It just outputs this:

 

2<!!!>Bill<!!!>notadded

 

 

instead of

 

1<!!!>Adam<!!!>added

2<!!!>Bill<!!!>added

 

 

any ideas why?

 

thanks in advance!

or if anyone can think of a more efficient way of doing it I would be very grateful!

 

What ID is unique, and so are the ID's in the array

 

I can't think of a way of mayching them up other than checking each new row against the whole array.

 

anyone? :P

SELECT * FROM data

 

 

there's a csv list of data that has been selected posted to the php file this is then exploded to an array.

 

I want this php script to echo ALL the data but give a different output on the data that has an ID which is in the array. (also I want it done in one query if possible)

This should work

 

<?php
$id_array = array(1,2);

while($row = mysql_fetch_assoc($result)) {

  if (in_array($row['id'],$id_array)) {
    
echo $row['id'] . '<!!!>' . $row['name'] .'<!!!>added<br />';

  } else {
    
echo $row['id'] . "<!!!>" . $row['name'] . "<!!!>notadded<br />";

  }
  
}
?>

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.