Jump to content

Exploded string from Db query


Gobbag

Recommended Posts

I'm scratching my head, not really a coder just muddled my way through from the Internet but this one has got me puzzled.

 

from a db query:-

 

$var9=$row["Closed_Days"]; this is a varchar(30) in a db, its value is can be  sun,mon,tue,wed,thu,fri,sat or any combination.

 

$days = explode("," $var9);
    $day1 = $days[0];
    $day2 = $days[1];
   $day3 = $days[2];
   $day4 = $days[3];
   $day5 = $days[4];
   $day6 = $days[5];
   $day7 = $days[6];

 

A user can input into the db from zero days to all days and the output is passed to a calendar/date picker to block any days when establishment isn't open. 

 

The output from the following is below:-

<?php        
echo $day1;
echo $day2;
echo $day3;
echo $day4;
echo $day5;
echo $day6;
echo $day7;
echo "      ";
echo $days[0];
echo $days[1];
echo $days[2];
echo $days[3];
echo $days[4];
echo $days[5];
echo $days[6];

?>

 

Gives the output " sunmontuewedthu sunmontuewedthufrisat"

 

$day6 and $day7 isn't being echoed yet they are in the array??

 

No matter what I do I can only get 5 of the seven  $days to pass to the calender.

Can't see what I'm doing wrong.

Thanks.

 

Link to comment
Share on other sites

That is a terrible way to store your data. Multiple values should not be stored in a single field. Instaead they should be stored in separate rows.

Rant over.

I am surprised your code executes at all - you have a syntax error in the first line (a missing comma)

$days = explode("," $var9);
                   ^
                   comma missing

Make sure your error reporting level is set to E_ALL and that error reporting is on (in php.ini file). If some days are not specified your code fails because of undefined indexes in the array.

Link to comment
Share on other sites

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.