1

Hope someone can help me on this.

I have this code.

$calendar->highlighted_dates = Array('2016-12-03', '2016-12-14', '2016-12-25' );

With it, i can have the dates highlighted in a calendar.

I'm Trying to get the same thing with a query using this code

$sql = mysql_query("SELECT * FROM event Order by Date");
              while($row = mysql_fetch_array($sql)){
                $Date.= "'".$row["Date"]."'".", "; 
            }
$arrayb=array("$Date");
$calendar->highlighted_dates = $arrayb;

If i echo $Date i get

'2016-12-03', '2016-12-14', '2016-12-25',

But if i Print_r ($arrayb) i get

Array([0]=>'2016-12-03', '2016-12-14', '2016-12-25',)

And nothing on Calendar. I guess the problem is the [0]=>, but i don't know how to remove it.

Thanks.

Final Code as suggested by Mark Baker:

$link=   mysqli_connect("$host", "$username", "$password","$db_name")or die("cannot connect"); 
 $dateArray = array();
   $sql = mysqli_query($link, "SELECT * FROM event Order by Date");
            while($row = mysqli_fetch_array($sql)){ $dateArray[] = $row["Date"]; }
$calendar->highlighted_dates = $dateArray;
3
  • 3
    Don't build a string of dates and cast that to an array, because then you'll have a sinqle array entry containing a long string..... build the array of dates as you iterate through the query resultset.... $dateArray = array(); while($row = mysql_fetch_array($sql)){ $dateArray[] = $row["Date"]; } Commented Feb 20, 2016 at 19:44
  • 2
    And stop using the old, deprecated MySQL extension..... it's 2016 now, switch to using MySQLi or PDO Commented Feb 20, 2016 at 19:45
  • Thanks Mark. Working perfect now. Commented Feb 20, 2016 at 19:55

2 Answers 2

3

You need to change your code as @Mark Baker suggested:-

$Date = array(); 

while($row = mysqli_fetch_array($sql)){ 
   $Date[] = $row["Date"]; 
}

Now you will get $Date as Array('2016-12-03', '2016-12-14', '2016-12-25') what you want.

Note:- mysql_* is deprecated now, Use mysqli_* or PDO.

Sign up to request clarification or add additional context in comments.

Comments

1

You haven't created a 3 value array:

Array('2016-12-03', '2016-12-14', '2016-12-25' );

You've created a 1 value array:

Array("'2016-12-03', '2016-12-14', '2016-12-25'");

Notice the 3 strings versus 1 string.

2 Comments

no, i have 3 strings separated by a comma. look - $Date.= "'".$row["Date"]."'".", ";
No, you have 3 substrings connected by a comma, they are all concatenated into one string...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.