Triggering query from clickable link

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hiphop
Forum Newbie
Posts: 2
Joined: Thu Apr 22, 2010 4:27 pm

Triggering query from clickable link

Post by hiphop »

Hi All,
I am new to Php/mysql and was working on a calendar time slot application. So far I have done calender & table (MYSQL DB) creation.

I would really appreciate if someone could help on solving issue below:

------when I click a date it will trigger a query, pull some data from db and display it on the same page.-----

Thank you in advance for your help!

Code: Select all

//Clickable Calendar Code

html>
<head>
        
 <title>Reservation Request</title>

</head>
<body>
<?php 


$monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
?>
<?php
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
?>
<?php 


$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];

$prev_year = $cYear;
$next_year = $cYear;

$prev_month = $cMonth-1;
$next_month = $cMonth+1;

if ($prev_month == 0 ) {
$prev_month = 12;
$prev_year = $cYear - 1;
}
if ($next_month == 13 ) {
$next_month = 1;
$next_year = $cYear + 1;
}

?>


<table width="200">
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="left">&nbsp;&nbsp;<a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>
<td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>" style="color:#FFFFFF">Next</a>&nbsp;&nbsp;</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<table width="100%" border="0" cellpadding="2" cellspacing="2">
<tr align="center">
<td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>M</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>W</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>F</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
</tr> 

<?php
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];


$newdate = date('Y-m-j',$timestamp);
$newdate = strtotime ( '-1 day' , strtotime ( $newdate ) ) ;
$newdate = date('Y-m-j',$newdate);

for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0 ) echo "<tr>\n";
if($i < $startday) echo "<td></td>\n";
else {
$newdate = strtotime ( '+1 day' , strtotime ( $newdate ) ) ;

$newdate = date ( 'Y-m-j' , $newdate );
echo "<td align='center' valign='middle' style='height:20px'>";

//I need to update the line below when a date get selected it will run a query in db and post the aviable time slots 
//for that day on this (Reservation Request) page


echo "<a href='".$newdate."'>".($i - $startday + 1) ."</a>";

echo "</td>\n";
}
if(($i % 7) == 6 ) echo "</tr>\n";
}

?>

</table>
</td>
</tr>
</table>


</body>
</html>



//db code 
//DB related code

$db = mysql_connect("mysql", "Test", "test")  or die(mysql_error());
//echo "Connected to mysqlL<br />";
mysql_select_db("Test" ) or die(mysql_error());
/*
//echo "Connected to Database";
$login = mysql_query("select * from tbuser) or die(mysql_error());  
$login1 = mysql_fetch_array( $login );
// Print out the contents of the entry 


while($login1 = mysql_fetch_array($login1)){
	echo $login1['username']. " - ". $login1['password'];
	echo "<br />";
}


//line that I think need to be udated

echo "<a href='".$newdate."'>".($i - $startday + 1) ."</a>";
Last edited by Benjamin on Thu Apr 22, 2010 4:48 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Triggering query from clickable link

Post by Christopher »

You can either use a regular link and have PHP regenerate the page with the changes (based on parameters I assume). Or use Ajax to load part data or HTML to update part of the page with Javascript.
(#10850)
hiphop
Forum Newbie
Posts: 2
Joined: Thu Apr 22, 2010 4:27 pm

Re: Triggering query from clickable link

Post by hiphop »

Thank you Christopher!

Is there any sample code that I can use?

Thanks!
Post Reply