Help with variables

Hi, I’m pretty new to PHP and I’m hoping you could lend me a hand. The code I have right now is below. I would like to do a while loop to automate things but from what I have read I can not but a variable in the code. I would like to do: $row_rsFlavors['$countL'] instead of $row_rsFlavors['1L'] or something similar.

[php]

<?php $count = 1; //start machine count at one while ($count <= $row_rsFlavors['machines']){ echo ''; echo ' '; echo '' . $row_rsFlavors['$countL']. ''; $result = mysql_query('SELECT * FROM flavors ORDER BY ID ASC') or die (mysql_error()); while ($row = mysql_fetch_assoc($result)) { echo ''. $row['Flavors'].'' . "\n\t"; } echo ''; echo ''.$count.''; echo ' '; echo ''.$row_rsFlavors['$countR'].''; $result = mysql_query('SELECT * FROM flavors ORDER BY ID ASC') or die (mysql_error()); while ($row = mysql_fetch_assoc($result)) { echo ''. $row['Flavors'].'' . "\n\t"; } echo ''; echo ''; $count++; } ?>

[/php]

hi before using $row_rsFlavors['$countL'] do the below thing

[php]
$countL = $count.‘L’;
than use
$row_rsFlavors[’$countL’];
//Note if your remove single quote it will also works. mens you can use these one too your have any error. $row_rsFlavors[$countL];
[/php]
Reply your feedback
~~SR~

The concatenation works but I don’t think you can do any variables in the database call. Is there anything else I can do?

if you want to display all records you can join a string by doing this
[php]
$option .= ‘’. $row[‘Flavors’].’’ . “\n\t”
[/php]

then you can just echo $option;

I personally don’t understand why you are calling the same query twice, or the difference between the L and R suffixes (i can only assume left and right?). Could you supply an example of the database data you are working with and an idea of what output you are after - I’m sure there’s a cleaner way of achieving your goal.

Sure, there’s nothing secret going on ;D. Below is what is in the database all the way through 10L and 10R.

store        1L         1R        2L         2R
LaCueva      Vanilla    Choc      Straw      NYCheesecake  
Bricklight   Vanilla    Choc      Coconut    HawaiianPineapple 

The reason I would like to do a loop to insert the data is because one store will have 10 machines and one store will have 8 machines.

What I want the loop to do is output the table below but one table will have 10 rows and another will have 8 rows. I hope that makes sense.

<table>
<tr>
    <td><img src="Vanilla.jpg"></td>
    <td>1</td>
    <td><img src="Choc.jpg"></td>
  </tr>
  <tr>
    <td><img src="Straw.jpg"</td>
    <td>2</td>
    <td><img src="NYCheesecake.jpg"></td>
  </tr>
....
</table>

This may help you on your way. The only issue is that the example table output is rather different from what your original code was doing?? Let me know if this helps you out at all, or how it needs to be tweaked, or if you want bits explained etc etc. (or if it doesn’t work! not tested locally - i’m daring like that)

[php]
$sql = “SELECT
id,store,1L,1R,2L,2R
FROM
flavors
ORDER BY
id ASC”;
$results = mysql_query($sql);
$stores = array();
while(false !== ($row = mysql_fetch_assoc($results)))
{
$id = $row[‘id’];
$store = $row[‘store’];
if(!in_array$store,$stores)) $stores[$store] = array();
$stores[$store][$id] = $row;
}

foreach($stores as $store => $data)
{
echo ‘

Store: "’.$store.’"

’;
echo ‘’;
foreach($data as $id => $info)
{
echo ‘’;
echo ‘’;
echo ‘’;
echo ‘’;
echo ‘’;
    echo '<tr>';
    echo '<td><img src="'.$info['2L'].'.jpg" /></td>';
    echo '<td>2</td>';
    echo '<td><img src="'.$info['2R'].'.jpg" /></td>';
    echo '</tr>';
}
echo '</tbody></table>';

}
[/php]

1

Smokey thank you very much for your assistance, I might be missing something but what I’m hoping to do is be able to choose between printing 8 and 10 rows dynamically while including the variable that is in the column. Maybe I need to reorganize my database…

Let me do a full database dump so you can see what I was trying to do. I was trying to do the the loop based on the column “machines”

Inserting the image wasn’t allowed, please click the link to see the database screenshot.
http://www.uswirlnm.com/database.png

Ok, so you want to (on a per row basis):

[ul][li]create a table based on the information in this row[/li]
[li]each row will be the ‘L’ & ‘R’ of each number, with the number in between[/li]
[li]the amount of rows created is decided by the number in the machines column[/li][/ul]

You had select boxes in the original post - are you ultimately wanting to create some sort of dropdown in the left and right column of each row? If so, what would be the contents - all of the flavours mentioned in the table?

I think the best thing to do is show you 8). The select boxes are being filled correctly on the test.php, just not displaying the current flavor for that machine.

LaCueva Store Links - 10 machines
http://www.uswirlnm.com/demo/test.php?store=LaCueva
http://www.uswirlnm.com/demo/flavorChooser2.php?store=LaCueva

Bricklight Store Links - 8 machines
http://www.uswirlnm.com/demo/test.php?store=Bricklight
http://www.uswirlnm.com/demo/flavorChooser2.php?store=Bricklight

Anyone else have an idea? Thanks.

I think you should rethink your database schema.

You should either have a machine count field in the store tables
or have another machine table which references the store.

There are times when variable variables come in handy, but I don’t see the need here, when a simple array will suffice.

Sorry, only found this by incident - don’t remember getting an update about this!

As the test.php is working for you - you say the only functionality missing is the fact that it is not defaulting to the values that are in the database? This would just be a case of when you are looping through the flavours to add them into each select box, check the value you are echoing into the option against the value of the related database value (1L, 1R) etc and if they match add the ’ selected=“selected”’ attribute to the option tag.

If you need actual code assistance on how to do the above, please post the code that is working for you in your test.php - do of course make me aware if just making it choose a default value isn’t the only thing wrong with that page’s productivity.

Sponsor our Newsletter | Privacy Policy | Terms of Service