Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I create a dropdown list for a start time selection and one for an end time. I have 3 arrays, time_group, time_name, and dotw. The end result looks like this:

Monday [dropdown] - [dropdown]
Tuesday [dropdown] - [dropdown]
Wednesday [dropdown] - [dropdown] etc etc

I am trying to check a sql row using preset variables further up on the actual page.

$char_avail_start_monday =  $row['char_avail_start_monday'];
$char_avail_stop_monday =  $row['char_avail_stop_monday'];

Now in the code below I loop through each day and create each of the two drop downs with the correct values for each. However I have an IF statement that if the db cell is empty do X, if = to Y do something etc. It seems my coding is not working right. I create ${char_avail_start_}.$dotw[$d] (ex: $char_avail_start_monday) and it is not pointing to the predefined row I want to view.

    //this is using my previous variable
    echo "row has ".$char_avail_start_monday ." ";
    //this is using my attempt at generating it and seeing it it can pull from the row
    echo "row has " .${char_avail_start_}.$dotw[$d] ." <br/>";

The output I get is:

"row has 2000 row has monday"

Which seems to be just the $dotw[$d] part. Is what I want to do not possible? Or am I just doing it wrong?

Full code is:

<?php

//CREATE START TIMES ARRAY
$time_group = array('0000', '0030', '0100', '0130', '0200', '0230', '0300', '0330', '0400', '0430', '0500', '0530', '0600', '0630', '0700'
  , '0730', '0800', '0830', '0900', '0930', '1000', '1030', '1100', '1130', '1200', '1230', '1300', '1330', '1400', '1430', '1500', '1530'
  , '1600', '1630', '1700', '1730', '1800', '1830', '1900', '1930', '2000', '2030', '2100', '2130', '2200', '2230', '2300', '2330');

//CREATE FORMATED TIME ARRAY
$time_name = array('Midnight', '12:30 AM', '01:00 AM', '01:30 AM', '02:00 AM', '02:30 AM', '03:00 AM', '03:30 AM', '04:00 AM', '04:30 AM', '05:00 AM', '05:30 AM', '06:00 AM', '06:30 AM', '07:00 AM'
  , '07:30 AM', '08:00 AM', '08:30 AM', '09:00 AM', '09:30 AM', '10:00 AM', '10:30 AM', '11:00 AM', '11:30 AM', 'Noon', '12:30 PM', '01:00 PM', '01:30 PM', '02:00 PM', '02:30 PM', '03:00 PM', '03:30 PM'
  , '04:00 PM', '04:30 PM', '05:00 PM', '05:30 PM', '06:00 PM', '06:30 PM', '07:00 PM', '07:30 PM', '08:00 PM', '08:30 PM', '09:00 PM', '09:30 PM', '10:00 PM', '10:30 PM', '11:00 PM', '11:30 PM');

$dotw = array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
?>

<table width="425" border="0">

<?      
//Loop through the days of the week *DOTW* array
$d = 0;
while ($d <= 6)  {
?>
    <tr>
      <?php
      //echo "test: " .${char_avail_start_}.$d;
          if (${char_avail_start_}.$d == ""){ 
              echo "<td width=\"190\" scope=\"col\"><span style=\"color: red\"><b>".ucfirst($dotw[$d])."</b></span></td>";
            } else {
              echo "<td width=\"190\" scope=\"col\">".ucfirst($dotw[$d])."</td>";
            }
      ?>

<td scope="col">
<?php

//here is my test code to see if its defined
if (${char_avail_start_}.$dotw[$d] == "2000"){ 
    echo "2000 FOUND\n";
  } else {

    //this is using my previous variable
    echo "row has ".$char_avail_start_monday ." ";
    //this is using my attempt at generating it and seeing it it can pull from the row
    echo "row has " .${char_avail_start_}.$dotw[$d] ." <br/>";
}

echo "<select name=\"char_avail_start_".$dotw[$d]."\" id=\"char_avail_start_".$dotw[$d]."\">\n";

//FIRST BLANK OPTION
if (${char_avail_start_}.$d == ""){
    echo "<option value=\"\" selected=\"selected\"></option>\n";
  } else {
    echo "<option value=\"\"></option>\n";
}

$i = 0;
while ($i <= 47)  {
    echo "<option value=\"".$time_group[$i]."\">".$time_name[$i]."</option>\n";
  $i++;
}
echo "</select>\n";

echo "-\n";

echo "<select name=\"char_avail_stop_".$dotw[$d]."\" id=\"char_avail_stop_".$dotw[$d]."\">\n";


//FIRST BLANK OPTION
if (${char_avail_stop_}.$d == ""){
    echo "<option value=\"\" selected=\"selected\"></option>\n";
  } else {
    echo "<option value=\"\"></option>\n";
}
$i = 0;
while ($i <= 47)  {
    echo "<option value=\"".$time_group[$i]."\">".$time_name[$i]."</option>\n";
  $i++;
}
echo "</select></td></tr>\n";

$d++;
}
?>
</table>
share|improve this question
1  
BTW: by using shorttags (<?) you are asking for troubles. – Marcin Orlowski Apr 3 at 13:15
there will be no trobles if configured once. – Anton Apr 3 at 13:18

2 Answers

If I understand correctly it would be better for you to create an array of days containing the available values:

$char_avail_start = array();
$char_avail_start['monday'] = $row['char_avail_start_monday'];
$char_avail_start['tuesday'] = $row['char_avail_start_tuesday];
...

You could then access the variable dynamically using something like this:

echo "row has " . $char_avail_start[$dotw[$d]] . "<br/>";
share|improve this answer
up vote 0 down vote accepted

So I was doing it wrong. Mucked with it more and found an error.
if (${char_avail_start_}.$dotw[$d] == ""){ blah }

Needed to be...
if (${char_avail_start_.$dotw[$d]} == ""){ blah }

Had my trailing } in the wrong spot.

It then is able to reference my previously defined variables.
$char_avail_start['monday'] = $row['char_avail_start_monday'];
etc...

and pull the data back from the sql server without issue.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.