newbie question on PHP and PostgreSQL
| From: | m | Date: | Thu, 08 May 2003 17:48:27 +0000 |
| Subject: | newbie question on PHP and PostgreSQL | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-146889@lists.php.net to get a copy of this message | ||
Hey All,
I'm hoping you may have seen this or may at least know of a
newsgroup/website I can hit that might help me explain why this is
happening...
Here's the scenario. I'm connecting to a PostgreSQL database and grabbing
all the schedule results for a given month. Then I'm looping over those
results. There are actually a ton of stupid loops going on... but the weird
thing is that after I go through the list once and then go back to the top
of the result set, the first value in the array (from the query) seems to
disappear!? Any clues?
I know that this code only prints the names of all bands for every day in
the schedule. My next task is to try to figure out how to match the date
associated with a row with the date that's being constructed in the loop.
But if anyone has any suggestions on that, it's just a bonus.
My comments below will detail my madness. Thanks in advance for any
suggestions!
Cheers,
-m
Here's the code, essentailly:
<?php
//-------------------------------database connection start
// Connecting, selecting database
include('database_connecter.php');
//-------------------------------database connection end
//-------------------------------see if the month and/or year have been
passed through the URL string
//-------------------------------begin set month and year variables
accordingly
$qry_string = $_SERVER['QUERY_STRING'];
parse_str($qry_string);
if ($month < 1 OR $month > 12) {
unset($month); }
if ($year < 1999 OR $month > 2099) {
unset($month); }
if (!isset($month)) {
$month = date("m");
}
if (!isset($year)) {
$year = date("Y");
}
$month_year = date(F) . ' ' . date(Y);
//-------------------------------end set month and year variables
accordingly
print "<!--Connected successfully-->";
//-------------------------------head information
print '<head><title>' . $month_year . ' at The
Casbah</title></head>';
//-------------------------------begin construct query to pull bands and
shows from database
$sql = 'select bandname, showdate from bands inner join bandshow on
bands.bandid ';
$sql = $sql . ' = bandshow.bandid inner join shows on bandshow.showid =
shows.showid ';
$sql = $sql . ' where bandshow.venueid = 1 and extract(month from showdate)
= ' . $month ;
$sql = $sql . ' order by shows.showdate, bandshow.bandorder';
//-------------------------------begin construct query to pull bands and
shows from database
$sch_result = pg_query($connection, $sql)
or die("Failed to grab schedule");
$rowcount = pg_num_rows($sch_result);
echo "<BR>";
echo "total of $rowcount band/show records in this month's schedule";
echo "<BR>";
echo "<table border=1>";
$numDaysInMonth = date("t", mktime(0, 0, 0, $month, 1, $year));
//-------------------------------loop number of times as days in current
month
//-------------------------------I know that this isn't perfect yet, but
it's just weird
//-------------------------------because after I run through this once and
reset the
//-------------------------------result witht he pg_result_seek command, it
seems to lose
//-------------------------------a value from the array. I think the array
is still the same size
//-------------------------------as it was before but maybe there's a better
way to do this?
for ($i=0; $i < $numDaysInMonth; $i++) {
echo '<tr><td>' . date("D", mktime(0, 0, 0, $month, ($i+1), $year))
. ' ' .
($i+1) . ' ' . '</td>';
echo '<td>';
//loop over result set
for($j=0; $j < $rowcount; $j++) {
$row = pg_fetch_array($sch_result);
//check each possible day in month agains each result
echo $row["bandname"] . ', ';
}
//reset($row);
pg_result_seek($sch_result, 0);
echo '</td></tr>';
}
echo "</table>";
?>