Jump to content

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>";
  }
?>

 

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.