Jump to content

[SOLVED] store mysql results in array, loop, but only show certain fields (almost there?)


Recommended Posts

My problem is when I use an array to store a record set, when I loop through, I can only display one field per row or everything in a row.  I can't figure out how to reference specific fields.  I've worked on this off and on for more than a month, and I haven't been able to do quite what I wanted.

 

<?php
$qu = "SELECT * from table1 where pnum > 100";

$result = mysql_query($qu) or die('Error getting result - mysql_error() = '.mysql_error());
$values = array();
while ($row = mysql_fetch_array ($result,MYSQL_ASSOC)) {
$values[$row['pnum']] = $row;
$columns = array_keys($row);
}
mysql_close($con);
//this displays the data as I would like to see, but it's not a loop!
echo $values[101][pnum]." - ".$values[101][name]."<br>";
echo $values[102][pnum]." - ".$values[102][name]."<br>";

echo $values[101][pnum]." - ".$values[101][pdesc]."<br>";
echo $values[102][pnum]." - ".$values[102][pdesc]."<br>";

//I want to display it dynamically, but can't figure out how to reference single fields
//I have tried several different ways
foreach ($values as $k => $v) { 
echo "k (this displays pnum) = ".$k;
echo "but I can't reference ".$k[pnum];
echo "<br>";
}
?>

perhaps I just need to do one query for one set of data, store in array A, then store another query in array B.  They're the same records, but different data needs to be displayed in different areas.

 

More details / other info:

I recently gave up asp for php.  in asp when working with record sets, I would:

 

select *

loop through all results and show A, B, C, and D

go back to the first record

loop through again but show A, B, F, and E

 

This probably wasn't the right way to do it, but it's all I could figure out back then.

 

I never figured out how to go back to the first record in php (but it would be a bonus to know).

 

Instead, I am trying to store my results in an array because it sounds like it might be good practice (perhaps a wise idea), and it might enable me to do multiple queries and store them in multiple arrays, then display the data. 

 

I could really use some help.

 

Thanks in advance.

<?php 
$qu = "SELECT * from table1 where pnum > 100";

$result = mysql_query($qu) or die('Error getting result - mysql_error() = '.mysql_error());
$values = array();
while ($row = mysql_fetch_assoc ($result)) {
$values[] = $row;
}

foreach ($values as $row)
{
    extract ($row);
    echo "$a $b $c $d <br>"; 
}

foreach ($values as $row)
{
    extract ($row);
    echo "$a $b $f $e <br>"; 
}
?>

 

BONUS : to start again at row 0

 

mysql_data_seek($result, 0)

 

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.