Var within a Var

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Var within a Var

Post by lloydie-t »

Maybe simple for somebody, Maybe not achievable, but "I" can't do it. I am trying to create a table, but somes of the vars are in a column which has the same names as the returned row. basically the script should replicate the following:

Code: Select all

//example data
quote.F5 = 1
part_code = F5

//what the code should look like if typed manually
     print "<tr>";
    print "<td >$row[part_name]</td>";
    print "<td>$row[F5]</td>"; 
    print "<td>$row[cost_1]</td>";
    print "<td>$parttotal</td>";
    print "</tr>";

//this is where I am, but it don't work.

        print "<tr>";
    print "<td >$row[part_name]</td>";
    print "<td>$row[(echo $row[part_code])]</td>"; 
    print "<td>$row[cost_1]</td>";
    print "<td>$fivetotal</td>";
    print "</tr>";
Can you help?
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

..it is generally a good idea to keey array variables outside strings/quotes..

Code: Select all

<?php
$beer = (
  'good' => 'Sam Adams',
  'fair' => 'Besties'
);
$i_like = 'good';
echo $beer[$i_like];
?>
So you are probably looking for something like
echo '<td>'.$row[$row['part_code']].'</td>';
User avatar
Sky
Forum Commoner
Posts: 71
Joined: Wed Oct 02, 2002 4:00 pm

Post by Sky »

Ok.... What are you trying to do? is this

Code: Select all

print "<tr>"; 
    print "<td >$row[part_name]</td>"; 
    print "<td>$row[!BARRING THIS!(echo $row[part_code])]</td>"; 
    print "<td>$row[cost_1]</td>"; 
    print "<td>$fivetotal</td>"; 
    print "</tr>";

going to be <pre>'d or is this actual code?
If not.. it should (maybe should isn't the right word.. we'll use 'should work') like this (I prefer echo to print)

Code: Select all

print "<tr>"; 
    print "<td >"/* split up direct string from vars with . */ . $row['part_name']."</td>"; 
    print "<td>".$row[$row['part_code']]."</td>"; 
    print "<td>".$row['cost_1']."</td>"; 
    print "<td>".$fivetotal."</td>"; 
    print "</tr>";
Thou I don't get where you're going with it....
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

Thanks sky. what I needed is now working. I am sure there is a better way, but this is whats working for know. I will show the rest of the code so you know what my problem was:

Code: Select all

$query = "SELECT alcatel_quote.*, alcatel_bits.* FROM alcatel_quote left join alcatel_bits on ((alcatel_bits.part_code = 'F5' and alcatel_quote.F5 >= 1) or (alcatel_bits.part_code = 'F6' and alcatel_quote.F6 >= 1) or (alcatel_bits.part_code = 'F7' and alcatel_quote.F7 >= 1) or (alcatel_bits.part_code = 'F8' and alcatel_quote.F8 >= 1) or (alcatel_bits.part_code = 'F9' and alcatel_quote.F9 >= 1) or (alcatel_bits.part_code = 'F10' and alcatel_quote.F10 >= 1) or (alcatel_bits.part_code = 'F11' and alcatel_quote.F11 >= 1) or (alcatel_bits.part_code = 'F12' and alcatel_quote.F12 >= 1) or (alcatel_bits.part_code = 'F13' and alcatel_quote.F13 >= 1) or (alcatel_bits.part_code = 'F14' and alcatel_quote.F14 >= 1))
where alcatel_quote.trunk = '$trunks' and alcatel_quote.extn = '$extn' and alcatel_quote.alog = '$alog' and alcatel_quote.net ='$internet'";
$queryResult = mysql_query($query);

$i=0;
while($row =mysql_Fetch_array($queryResult)) 

{ 
 $total = 1 * $row[cost];
    print "<tr>";
    print "<td>$row[descript]</td>";
    print "<td>1</td>"; 
    print "<td>$row[cost]</td>";
    print "<td>$total</td>";
    print "</tr>";
    
    print "<tr>"; 
    print "<td >". $row['part_name']."</td>"; 
    print "<td>".$row[$row['part_code']]."</td>"; 
    print "<td>".$row['cost_1']."</td>";
    $parttotal =  $row[$row['part_code']] * $row['cost_1'];
    print "<td>$parttotal</td>"; 
    print "</tr>"; 

   
}

    
?>
Post Reply