Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

organise result from php mysql query 1

Status
Not open for further replies.

kzn

MIS
Jan 28, 2005
209
GB
$query = "select sid,software from products order by software asc";
$results = mysql_query($query);

while($row = mysql_fetch_array($results, MYSQL_ASSOC)) {

echo '<td align="left"><input type="checkbox" name="software[]" value="'.$row['sid'].'" />'.$row['software'].'</td>';
}

I have two columns and i would like them to appear side by side. But with the above method everything is in its own td.

I want the result to be
<tr>
<td><input type="checkbox" name="software[]" value=""4" />office 2003</td>
<td><input type="checkbox" name="software[]" value="1" />winzip</td>
</tr>

What happens if the results are uneven.... then what happens to the second <td> ?
 
Code:
<?php
$query = "select sid,software from products order by software asc";
$results = mysql_query($query) or die (mysql_error());
$numRecords = mysql_num_rows($results);
$i = 0;
echo "<table>\r\n"; //start table
while ($i< $numRecords){
	echo "<tr>\r\n"; //start row
	//get first col
	$row = mysql_fetch_assoc($results);
	//output first col
	echo <<<HTML
	<td align="left">
		<input type="checkbox" name="software[]" value="{$row['sid']}" />{$row['software']}
	</td>

HTML;
	$i++; //increment counter
	
	//test second row
	if ($row = mysql_fetch_assoc($results)){
		echo <<<HTML
	<td align="left">
		<input type="checkbox" name="software[]" value="{$row['sid']}" />{$row['software']}
	</td>

HTML;
	} else {
		echo <<<HTML
	<td align="left">
		&nbsp;
	</td>

HTML;
	}
	echo "</tr>\r\n"; //close the row
	
}
echo "</table>\r\n"; //close the table
 
Thank you very much for your help. One problem with the your script:
$i++; //increment counter needs to be
$i+=2; //increment counter.

Once again thank you for your help.
 
i don't see why i'm afraid. my code looks right to me. why do you want to assign the value of 2 to $i?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top