Mestre1966
Mestre1966

Reputation: 33

array of dates with query

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;

Upvotes: 1

Views: 98

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

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.

Upvotes: 3

Devon Bessemer
Devon Bessemer

Reputation: 35337

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.

Upvotes: 1

Related Questions