Jump to content

[SOLVED] while statement


eagleweb

Recommended Posts

I presently have an include page with this code on it. On the calendar pages, it uses this info to color the dates on a calendar.
[code]
<?php
$dayColor = 'e6e1d3';
$weekendColor = '666666';
$mincolor = '00CC66';
$maxcolor = 'cc0000';
?>

I want to pull this info from the database instead. This is what I have:

<?php
require_once('mysql_connect.php');
$calQuery = mysql_query ( "SELECT * FROM calendar", $mysql_connect);
while ( $rowcal = mysql_fetch_array ( $calQuery ) )
   {
      echo " ".$rowcal['code']." = ' ".$rowcal['info']." '<br>";
   }
?>

This gives me nice list of the items in the db - perfectly. But, it is an echo statement and it just echos the info on each of the pages instead of coloring the calendar dates.

The db has the fields: id, code (which holds the $dayColor, etc), info (which holds the colors e6e1d3, etc)

 

How do i get this to list all of the info without 'echoing' it so they are usable on the calendar pages as

<?php echo $dayColor; ?>

?

 

Thanks in advance![/code]

 

 

Link to comment
https://forums.phpfreaks.com/topic/71839-solved-while-statement/
Share on other sites

I think you're looking for variable variable names.

 

If I understood you correctly, the "code" column stores the name of the variable, and the "info" colums stores the value that should be stored in that variable.

 

<?php
require_once('mysql_connect.php');
$calQuery = mysql_query ( "SELECT * FROM calendar", $mysql_connect);
while ( $rowcal = mysql_fetch_array ( $calQuery ) )
  {
     $var_name = $rowcal['code'];
     $$var_name = $rowcal['info'];
     //echo " ".$rowcal['code']." = ' ".$rowcal['info']." '<br>";
  }
?>

 

That will do what you want, but I think you'd be better off with something like this.  It helps organize your data.

 

<?php
require_once('mysql_connect.php');
$calQuery = mysql_query ( "SELECT * FROM calendar", $mysql_connect);

$colors = Array();

while ( $rowcal = mysql_fetch_array ( $calQuery ) )
  {
     $colors[$rowcal['code']] = $rowcal['info'];
     //echo " ".$rowcal['code']." = ' ".$rowcal['info']." '<br>";
  }
?>

 

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.