Forum Moderators: coopster

Message Too Old, No Replies

$myrow[$var]?

         

busystock

8:06 pm on Mar 1, 2010 (gmt 0)

10+ Year Member



i have mysql output stored in $myrow, instead of accessing like $myrow['first_name'], i have to loop through the column names via variable like $myrow[$var], $var is an array variable of table column names.

my problem is i cannot make $myrow[$var] work...

ty

Matthew1980

8:49 pm on Mar 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there busystock,

Have you tried: $myrow['$var'] at all? I could be wrong, but that seems plausible to me..

Cheers,

MRb

busystock

9:10 pm on Mar 1, 2010 (gmt 0)

10+ Year Member



i tried, not working

Matthew1980

9:20 pm on Mar 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there busystock,

Sorry, just tested a new suggestion out:-

$test = array();
$var = "jimmi";

$test["$var"] = "Hendrix";

print_r($test);

outputs:-

Array ( [jimmi] => Hendrix )


So double quotes not single..

Cheers,

MRb

rocknbil

10:39 pm on Mar 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't need quotes at all to do this. :-) Truthfully, you don't even need the field names, see option 2 below.

$fields = get_table_names('tablename');
// the above is just how I do it, where get_table_names
// returns an array of the table 'tablename';
//
// Create a null for $rows, nix concatenation errors
$rows=NULL;
// Do your connect, select, store resource in $result . . .

while ($myrow=mysql_fetch_array($result)) {
$rows .= '<tr>';
foreach ($fields as $var) {
$rows .= '<td>' . $myrow[$var] . '</td>';
}
$rows .= '</tr>';
}
//
if ($rows) {
echo '<table>';
// The first is the column HEADER for the table.
// Note ONE LESS than count, arrays are zero based
// and a count is one more than the last index.
$rows .= '<tr>';
for ($i=0;$i<count($fields)-1;$i++) {
echo '<td>' . $fields[$i] . '</td>';
}
echo '</tr>';
echo $rows;
echo '</table>';
}
else { echo '<p>No results were found.</p>'; }


Option 2, why you don't need the field names to access the values: PHP stores both the associative array and the indexed array in mysql_fetch_array(). So you can rip through it like any other list array:


while ($myrow=mysql_fetch_array($result)) {
$rows .= '<tr>';
for ($i=0;$i<count($rows)-1;$i++) {
$rows .= '<td>' . $myrow[$i] . '</td>';
}
$rows .= '</tr>';
}
// the same if/else follows


The only place you need the field names is in the table header.

Matthew1980

10:49 pm on Mar 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Rocknbil,

That's me told then ;-p I guess I wasn't looking at the bigger issue there, just concerned with fixing the short term.

Cheers,
MRb

Readie

12:08 am on Mar 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



 for ($i=0;$i<count($rows)-1;$i++) { 
$rows .= '<td>' . $myrow[$i] . '</td>';
}


I was under the impression it was bad practice to include the count function inside the for loop like this, as each time the loop is run it is re-doing the count function - which slows the script down.

rocknbil

1:23 am on Mar 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe so. Nah, it's PHP - *probably* so. Put it in a variable. :-) Just an example of a way to do it, typed on the fly . . .

$ct = count($fields)-1;

busystock

2:20 pm on Mar 2, 2010 (gmt 0)

10+ Year Member



thanks for all replays. i made a mistake eleswhere to cause my problem.

cheers.