Jump to content

Multidimensional array help


raytri

Recommended Posts

I have a page that lets visitors pick up to three machines from a checklist, and then generate a table comparing the features of those machines.

 

The SQL query for the table is assembled thusly:

 

<?php 
//capture ids of selected machines
$atmID = $_POST['atmid'];

//Assemble query variable (makes sure query works even if fewer than three machines selected), and count number of machines to get table column count
$atmlist = $atmID[0];
$comparecount = 2;
if($atmID[1]) {$atmlist .= ",".$atmID[1]; $comparecount ++;}
if($atmID[2]) {$atmlist .= ",".$atmID[2]; $comparecount ++;}

//Query for assembling comparison chart
$selectSQL = "SELECT * FROM ATMcompare WHERE `atmID` IN($atmlist)";
$selectQuery = mysql_query($selectSQL);
?>

 

So far so good. If I fetch the array from the above query, I'll have an array containing up to three subarrays, one for each machine selected.

 

But I can't figure out how to echo the subarray values directly to a table, something like this:

 

<?php $selectArray = mysql_fetch_array($selectQuery); ?>

<tr><td><p><strong>ATM model</strong></p></td><td><?php echo $selectArray[0][1]; ?></td><td><?php echo $selectArray[1][1]; ?></td><td><?php echo $selectArray[2][1]; ?></td></tr>

 

I've made the page work, but only by using a while statement to loop through the array for each feature, and rewinding the pointer with mysql_data_seek after each loop:

 

<tr><td><p><strong>ATM model</strong></p></td>
<?php while ($item = mysql_fetch_array($selectQuery)) {
echo "<td><p><strong>".$item['Maker']." ".$item['Model']."</strong></p></td>"; }
mysql_data_seek($selectQuery,0); ?>
</tr> 

 

I prefer the first approach because it makes for cleaner code, appears to involve a lot less processing, and would prove I understand array structures. :)

 

Any tips?

Link to comment
https://forums.phpfreaks.com/topic/199936-multidimensional-array-help/
Share on other sites

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.