Jump to content

[SOLVED] PHP arrays into arrays need help


ashton321

Recommended Posts

Hello

I am trying to highlight the days on my calendar based on the dates that i have in my database.  Currently I can only get it to display the last element in the database which leads me to believe that i have an error in my arrays

 

	
<?php
             ## My database
             $query = mysql_query("SELECT * FROM speaking_engagements");
while($result = mysql_fetch_array($query)){
$dates = $result['date'];
$dates = explode('-',$dates);
$dates = $dates[2];
}
#$dates = explode('-',$dates);
#$dates = $dates[2];
#$numdays = count($dates);
$time = time(); 
$today = date('j',$time); 
$days = array(
	$today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger;">'.$today.'</span>'),
	$dates=>array('#'), 
        8=>array('#'),  ## a hard coded one that works
    );
$today = getdate();
$year = $today['year'];
$month = $today['mon'];
echo '<div id="calendar">';
    echo generate_calendar($year, $month, $days, 3, NULL, '/weblog/archive/2004/Jan'); 
echo '</div>';
?>

Link to comment
Share on other sites

	
$dates = array();
$query = mysql_query("SELECT date_column FROM speaking_engagements");
while($result = mysql_fetch_array($query)){
   	$date = $result['date'];
   	$date_parts = explode('-',$dates);
   	$dates[] = $date_parts[2];
}

 

You should never use SELECT * in an application. It's trouble, besides, it looks like you only need one column anyway. Why waste the resources? Replace "date_column" with the name of your date column.

Link to comment
Share on other sites

Yeah, there is no reason to SELECT * I did that when I was frustrated and couldn't get it to work.  Now I have tried what you suggested and now I am not getting any results to show, not even the last.  I tried to put it into the calendar array and there was no error, but i tried to echo $dates and it output Array and when I tried to output $dates[0] there was nothing.  Here is the updated code i tried.

 

<?php 
#database connection here
$dates = array();
$query = mysql_query("SELECT date FROM speaking_engagements");
while($result = mysql_fetch_array($query)){
	$date = $result['date'];
	$date_parts = explode('-',$dates);
	$dates[] = $date_parts[2];
}
$time = time(); 
$today = date('j',$time); 
$days = array(
	$today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger;">'.$today.'</span>'),
	$dates[0]=>array('#'),
	$dates[1]=>array('#'),
	$dates[2]=>array('#'),
	$dates[3]=>array('#'),		
        8=>array('#'),
    );
echo $dates[0];

$today = getdate();
$year = $today['year'];
$month = $today['mon'];
echo '<div id="calendar">';
    echo generate_calendar($year, $month, $days, 3, NULL, '#'); 
echo '</div>';
?> 

Link to comment
Share on other sites

You could just use DayOfMonth(`date`) in the SQL to get the day number instead of picking it apart in PHP:

SELECT DayOfMonth(`date`) AS CalDay FROM speaking_engagements

 

Note: Are the back-ticks still necessary?  I think so, but I'm not sure.  You should always avoid using reserved words for column names, table names, database names, etc.  It just makes things harder on you when accessing the table.

Link to comment
Share on other sites

Found the problem now!

<?php	$dates = array();
$query = mysql_query("SELECT date FROM speaking_engagements");
while($result = mysql_fetch_array($query)){
	$date = $result['date'];
	$date_parts = explode('-',$dates);
	$dates[] = $date_parts[2];
}
?>

 

is now

<?php	$dates = array();
$query = mysql_query("SELECT eventdate FROM speaking_engagements") or die(mysql_error());

while($result = mysql_fetch_array($query)){
	$date = $result['eventdate'];
	$date_parts = explode('-',$date);
	$dates[] = $date_parts[2];
}?>

 

I changed the column name in the database from the keyword date and that was not the issue.  Then I look at what I was exploding, and saw that i was exploding dates with an "s" when it needed to be without the "s".  Thanks for all the help with the arrays

Link to comment
Share on other sites

It has been driving me nuts for a few days now!  But, now that I got my data out, I need to make 01 for a day turn into 1 but I donk know how to run like and if statement or something on the entire array because I will not know its position in the array?

 

You can either use ltrim: $number = ltrim($number, '0');

 

Or you can just cast your $number variable as (int) and the zero will be removed. Your choice.

Link to comment
Share on other sites

Sorry to be a pain here but im not that great with arrays.  i tried doing the int cast and it wasnt working so I tried the ltrim and now it is echoing A r r a as my array rather than the numbers.

<?php $dates = array();
$query = mysql_query("SELECT eventdate FROM speaking_engagements") or die(mysql_error());

while($result = mysql_fetch_array($query)){
	$date = $result['eventdate'];
	$date_parts = explode('-',$date);
	$dates[] = $date_parts[2];
}
$dates = ltrim($dates, '0');
$time = time(); 
$today = date('j',$time); 
$days = array(
	$today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger;">'.$today.'</span>'),
	$dates[0]=>array('#'),
	$dates[1]=>array('#'),
	$dates[2]=>array('#'),
	$dates[3]=>array('#'),		
        );
echo "$dates[0]  ";
echo "$dates[1]  ";
echo "$dates[2]  ";
echo "$dates[3]  ";

Link to comment
Share on other sites

you cant apply it to an array like that. you can do a few things.

 

while($result = mysql_fetch_array($query)){
      $date = $result['eventdate'];
      $date_parts = explode('-',$date);
      $dates[] = ltrim($date_parts[2],'0');
   }

 

Or you can setup a foreach loop by reference afterwards to do it. Or you could even use array_map. Best case is above though, its the fastest.

Link to comment
Share on other sites

ok thank you it works! :)

 

one last question is it possible to use some time of loop in

<?php $days = array(
	$today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger;">'.$today.'</span>'),
	$dates[0]=>array('#'),
	$dates[1]=>array('#'),
	$dates[2]=>array('#'),
	$dates[3]=>array('#'),		
        );?>

for when I add elemts to the database they all get displayed and i dont have to change the script?

Link to comment
Share on other sites

I'll try.  This is a list of days that are going to become links on a calendar once i get it to work.  The calendar makes the link based on the

<?php $days = array(
	$today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger;">'.$today.'</span>'),
	$dates[0]=>array('#'),
	$dates[1]=>array('#'),
	$dates[2]=>array('#'),
	$dates[3]=>array('#'),		
        );?>

array.  i would like to know if I am allowed to put like a for or while type loop inside the $days array to make every element of the $dates array show up.  right now i had to add

$dates[0]=>array('#'),
	$dates[1]=>array('#'),
	$dates[2]=>array('#'),
	$dates[3]=>array('#'),

for each element.  i would like it to be that if i add a fifth element to the database it also becomes a link on the calendar without me going in and adding

$dates[4]=>array(#),

 

Hope that is more clear

Link to comment
Share on other sites

I dont have much experience with foreach loops and i got

Parse error: syntax error, unexpected ';', expecting ')' in /homepages/44/d263144020/htdocs/webdev/inc/sidebar.php on line 15

 

when I tested

$days = array(
	$today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger;">'.$today.'</span>');
	foreach($dates as $key => $value) {
	$days[$key] = array('#');
	});

Link to comment
Share on other sites

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.