Jump to content

php bi-dimensional array


vladj

Recommended Posts

Hi,

 

I'm new to both PHP and this forum.

I need to display a calendar with images from a database, and I found a script that does this perfectly.

Problem is that some of the days from the mysql database, have more than one image, reffered from a field named "crt". The files are loaded from aan images directory, using the format, day_month_crt.jpg. Example 13_2_1.jpg or 13_2_2.jpg, meaning the first and the second image from 13rd february. The crt coresponds with same value in the database. Each image also has a title in the database, in the name field. The problem is I have multiple records per day.

 

I need to include the crt array in the days of month array, in order to loop through the images. And I don't know how to do that, with this script. Here it is:

 

<?php

 

 

 

function getEventDays($month, $year) {

 

$days = array();

$crt = array();

 

  $sql = mysql_query("SELECT crt,  image, day(data) AS day FROM calendar WHERE image>0 and month(data) = '$month' AND year(data) = '$year'");

 

  if (mysql_num_rows($sql) > 0) {

  while ($row = mysql_fetch_array($sql))

$days[] = $row['day'];

$crt[] = $row['crt'];

  }

 

  return $days;

 

}

 

function drawCalendar($month, $year) {

  // set variables we will need to help with layouts

  $first      = mktime(0,0,0,$month,1,$year); // timestamp for first of the month

  $offset      = date('w', $first); // what day of the week we start counting on

  $daysInMonth = date('t', $first);

  $luna  = date('F', $first);

  $weekDays    = array('D', 'L', 'Ma', 'Mi', 'J', 'V', 'Sa');

  $eventDays  = getEventDays($month, $year);

 

 

  // Start drawing calendar

  $out  = "<table style=\"border-collapse:collapse;text-align:bottom;width:780px;height:800px\" id=\"myCalendar\">\n";

  $out .= "<tr><th colspan=\"7\">$luna</th></tr>\n";

  $out .= "<tr>\n";

  foreach ($weekDays as $wd) $out .= "<td style=\"font-weight:bold;text-align:center;border:2px solid #C8B4AB;\" class=\"weekDays\">$wd</td>\n";

 

  $i = 0;

  for ($d = (1 - $offset); $d <= $daysInMonth; $d++) {

    if ($i % 7 == 0) $out .= "<tr>\n"; // Start new row

    if ($d < 1) $out .= "<td style=\"border:1px solid #C8B4AB;vertical-align:bottom;\" class=\"nonMonthDay\"></td>\n";

    else {

      if (in_array($d, $eventDays)) {

        $out .= "<td style=\"border:1px solid #C8B4AB;vertical-align:bottom;height:160px\" class=\"monthDay\">\n";

        $out .= "<a href=\"/images/".$d."_".$month."_1.jpg\" rel=\"thumbnail\"><img src=\"/images/".$d."_".$month."_1.jpg\" width=\"90\"  /></a><br />$d\n";

        $out .= "</td>\n";

      } else $out .= "<td style=\"border:1px solid #C8B4AB;vertical-align:bottom;width:90px;height:160px\" class=\"monthDay\">$d</td>\n";

    }

    ++$i; // Increment position counter

    if ($i % 7 == 0) $out .= "</tr>\n"; // End row on the 7th day

  }

 

  // Round out last row if we don't have a full week

  if ($i % 7 != 0) {

    for ($j = 0; $j < (7 - ($i % 7)); $j++) {

      $out .= "<td class=\"nonMonthDay\"> </td>\n";

    }

    $out .= "</tr>\n";

  }

 

  $out .= "</table>\n";

   

    return $out;

}

 

$year  = isset($_GET['year']) ? $_GET['year'] : date('Y');

$month = isset($_GET['month']) ? $_GET['month'] : date('n');

 

 

 

echo drawCalendar($month, $year);

 

// Previous month link

$prevTS = strtotime("$year-$month-01 -1 month"); // timestamp of the first of last month

$pMax = date('t', $prevTS);

$pDay = ($day > $pMax) ? $pMax : $day;

list($y, $m) = explode('-', date('Y-n', $prevTS));

echo "<p>\n";

echo "<a href=\"?month=$m\">« Prev</a> |\n";

 

// Next month link

$nextTS = strtotime("$year-$month-01 +1 month");

$nMax = date('t', $nextTS);

$nDay = ($day > $nMax) ? $nMax : $day;

list($y, $m) = explode('-', date('Y-n', $nextTS));

echo "<a href=\"?month=$m\">Next »</a>\n";

echo "</p>\n";

 

// Calendar is done, let's list events for selected day

$sql = mysql_query("SELECT * FROM calendar WHERE  data = '".$year."-".$month."-".$day."'");

 

if (mysql_num_rows($sql) > 0) {

  while ($e = mysql_fetch_array($sql)) {

    echo "<p>$e[name]</p>";

  }

}

?>

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/38305-php-bi-dimensional-array/
Share on other sites

You can make a multiD array this way with that info and loop through it:

 

$sql = mysql_query("SELECT crt,  image, day(data) AS day FROM calendar WHERE image>0 and month(data) = '$month' AND year(data) = '$year'");

while ($row = mysql_fetch_array($sql))

{

$dayNum =[$row['day'];

$days[$dayNum] = $row['crt'];

}

 

foreach($days as $key=>$value)

{

echo $key .'_'. $month .'_'. $value .'.jpg'; //shout output 13_2_1.jpg format

}

 

 

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.