call or create session variable in SUM - php

I am not very familiar with PHP or MySQL, but after researching here I have been able to learn and get very close to what I need and build my first sum query. I am trying to read the database and sum values based on several variables.
I need the reservation_pax from the reservations table where reservation_time is 8:00 where reservation_hidden = 0 and reservation_date is something. I can manually enter the date and it works. I am now trying to use a session code already in the script or find a way to to dynamically add based on selected date.
Here is the code I have working without the dynamic aspect or session.
$result = mysql_query("SELECT SUM(reservation_pax)
FROM reservations
WHERE reservation_time = '8:00:00'
AND reservation_date = '2014-10-27'
AND reservation_hidden ='0'") ;
if ($result === false) {
die(mysql_error()); // TODO: better error handling
}
while($row = mysql_fetch_array($result)) {
echo $row['SUM(reservation_pax)'];
}
Here is the full code of the page where I entered the above addition. Can anyone help me figure out how to call the selected date rather than having to manually enter.
<!-- Begin reservation table data -->
<br/>
<table class="global resv-table-small" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<?php
echo "<td class='noprint'> </td>";
echo "<td>Time</td>";
echo "<td>Guests/Type</td>";
echo "<td>Name</td>";
echo "<td>Special Instructions/Notes</td>";
echo "<td class='noprint'>Table</td>";
echo "<td class='noprint'>Status</td>";
echo "<td class='noprint'>Created</td>";
echo "<td class='noprint'>Details/Delete</td>";
echo "</tr>";
// Clear reservation variable
$reservations ='';
if ($_SESSION['page'] == 1) {
$reservations = querySQL('all_reservations');
}else{
$reservations = querySQL('reservations');
}
// reset total counters
$tablesum = 0;
$guestsum = 0;
if ($reservations) {
//start printing out reservation grid
foreach($reservations as $row) {
// reservation ID
$id = $row->reservation_id;
$_SESSION['reservation_guest_name'] = $row->reservation_guest_name;
// check if reservation is tautologous
$tautologous = querySQL('tautologous');
echo "<tr id='res-".$id."'>";
echo "<td";
// daylight coloring
if ($row->reservation_time > $daylight_evening){
echo " class='evening noprint'";
}else if ($row->reservation_time > $daylight_noon){
echo " class='afternoon noprint'";
}else if ($row->reservation_time < $daylight_noon){
echo " class='morning noprint'";
}
echo " style='width:10px !important; padding:0px;'> </td>";
echo "<td id='tb_time'";
// reservation after maitre message
if ($row->reservation_timestamp > $maitre['maitre_timestamp'] && $maitre['maitre_comment_day']!='') {
echo " class='tautologous' title='"._sentence_13."' ";
}
echo ">";
echo "<strong>".formatTime($row->reservation_time,$general['timeformat'])."</strong></td>";
echo "<td id='tb_pax'><strong class='big'>".$row->reservation_pax."</strong> <span class='noprint'>";
printType($row->reservation_hotelguest_yn);
//echo "<img src='images/icons/user-silhouette.png' class='middle'/>";
echo "</span></td><td style='width:10%' id='tb_name'><span class='noprint'>".printTitle($row->reservation_title)."</span><strong> <a id='detlbuttontrigger' href='ajax/guest_detail.php?id=".$id."'";
// color guest name if tautologous
if($tautologous>1){echo" class='tautologous tipsy' title='"._tautologous_booking."'";}
echo ">".$row->reservation_guest_name."</a></strong>";
// old reservations symbol
if( (strtotime($row->reservation_timestamp) + $general['old_days']*86400) <= time() ){
echo "<img src='images/icons/clock-bolt.png' class='help tipsyold middle smicon' title='"._sentence_11."' />";
}
// recurring symbol
if ($row->repeat_id !=0) {
echo " <img src='images/icons/loop-alt.png' alt='"._recurring.
"' title='"._recurring."' class='tipsy' border='0' >";
}
echo"</td><td style='width:10%' id='tb_note'>";
if ($_SESSION['page'] == 1) {
echo $row->outlet_name;
}else{
echo $row->reservation_notes;
}
echo "</td>";
if($_SESSION['wait'] == 0){
echo "<td class='big tb_nr' style='width:85px;' id='tb_table'><img src='images/icons/table_II.png' class='tipsy leftside noprint' title='"._table."' /><div id='reservation_table-".$id."' class='inlineedit'>".$row->reservation_table."</div></td>";
}
echo "<td class='noprint'><div>";
getStatusList($id, $row->reservation_status);
echo "</div></td>";
echo "<td class='noprint'>";
echo "<small>".$row->reservation_booker_name." | ".humanize($row->reservation_timestamp)."</small>";
echo "</td>";
echo "<td class='noprint'>";
// MOVE BUTTON
// echo "<a href=''><img src='images/icons/arrow.png' alt='move' class='help' title='"._move_reservation_to."'/></a>";
// WAITLIST ALLOW BUTTON
if($_SESSION['wait'] == 1){
$leftspace = leftSpace(substr($row->reservation_time,0,5), $availability);
if($leftspace >= $row->reservation_pax && $_SESSION['outlet_max_tables']-$tbl_availability[substr($row->reservation_time,0,5)] >= 1){
echo" <a href='#' name='".$id."' class='alwbtn'><img src='images/icons/check-alt.png' name='".$id."' alt='"._allow."' class='help' title='"._allow."'/></a> ";
}
}
// EDIT/DETAIL BUTTON
echo "<a href='?p=102&resID=".$id."'><img src='images/icons/pen-fill.png' alt='"._detail."' class='help' title='"._detail."'/></a> ";
// DELETE BUTTON
if ( current_user_can( 'Reservation-Delete' ) && $q!=3 ){
echo"<a href='#modalsecurity' name='".$row->repeat_id."' id='".$id."' class='delbtn'>
<img src='images/icons/delete.png' alt='"._cancelled."' class='help' title='"._delete."'/></a>";
}
echo"</td></tr>";
$tablesum ++;
$guestsum += $row->reservation_pax;
}
}
?>
</tbody>
<tfoot>
<tr style="border:1px #000;">
<td class=" noprint"></td><td></td>
<td colspan="2" class="bold"><?php echo $guestsum;?> <?php echo _guest_summary;?></td>
<td></td>
<td colspan="2" class="bold"><?php echo $tablesum;?> <?php echo _tables_summary;?></td>
<td colspan="2" class="bold"><?php
$result = mysql_query("SELECT SUM(`reservation_pax`) FROM `reservations` WHERE `reservation_time` = '8:00:00' AND `reservation_date` = '{$_SESSION['selectedDate']}' AND `reservation_hidden` ='0'") ;
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while($row = mysql_fetch_array($result))
{
echo $row['SUM(reservation_pax)'];
}
?>
<?php echo '/ 40 ', _guest_summary, ' -8:00 AM';?></td>
<td></td>
<?php
if($_SESSION['wait'] == 0){
//echo "<td></td>";
}
?>
</tr>
</tfoot>
</table>
<!-- End reservation table data -->

you can use alias field, as:
$result = mysql_query("SELECT SUM(reservation_pax) AS reserve_sum
FROM reservations WHERE reservation_time = '8:00:00'
AND reservation_date = '2014-10-27'
AND reservation_hidden ='0'"
) ;
....
and get access it as:
while($row = mysql_fetch_array($result))
{
echo $row['reserve_sum'];
}

$result = mysql_query("SELECT SUM(reservation_pax) FROM reservations WHERE reservation_time = '8:00:00' AND reservation_date = '{$_SESSION['selectedDate']}' AND reservation_hidden ='0'") ;
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while($row = mysql_fetch_array($result))
{
echo $row['SUM(reservation_pax)'];
}
?>

Related

How to check table cell in database and output results?

I am bit new to php and sql.
I am simply reading from a database table and echoing images to a page. The images are then styled with a text aligned in the middle saying 'Play Video'.
In the table 'adcTable' some entries do not have a video stored.
I would like to have the rows/entries with video say 'Play Video' and the ones without just show the image.
Not to sure how bess to explain. Hope to get some assistance.
<php
$sql = "SELECT client_id, images, video FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["images"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Play Video</div>
</div>
</div>
</td>";
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
echo "</tr></table>";
?>
Thanks guys, I was able to use the example provided by BusinessPlanQuickBuilder to solve.
$sql = "SELECT client_id, files, file FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
if ($row['file'] == "VidUploads/"){
echo "<td>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
</td>";
echo '<script type="text/javascript">',
'$( ".text" ).css( "border", "3px solid red" );',
'</script>';
} else{
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Video</div>
</div>
</div>
</td>";
}
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
}
echo "</tr></table>";
?>
Use if empty condition on video column. This is the fundamental code, add your formatting as required.
<?php
while($row = $result->fetch_assoc()) {
if (empty ($row['video']) ) {
echo $row ["images"];
} else {
echo "Play Video";
}
}
?>
SO reference to related post

Can't make it appear in the correct way PHP

we have an assignment for today in which we have to echo some data of the database. The data is month, case, vehicle_id. In a month there can be many cases and in a case there can be many vehicle_ids. What i have achieve to do is to show it in the following way
month st_case veh_id
1 10001 1000011
1 10002 1000021
1 10002 1000022
2 10058 1000581
using this code:
<table border="1">
<tr>
<th>month</th>
<th>st_case</th>
<th>veh_id</th>
</tr>
<?php
// Loop on rows in the result set.
for($ri = 0; $ri < $numrows; $ri++) {
echo "<tr>\n";
$row = pg_fetch_array($result, $ri);
echo " <td>", $row["month"], "</td>
<td>", $row["st_case"], "</td>
<td>", $row["veh_id"], "</td>
</tr>
";
}
pg_close($link);
?>
</table>
The problem is that what i really want to do is to make it show for each month the cases and for each case the vehicles.
1
10001
1000011
10002
1000021
1000022
2
10058
1000581
I tried to do something like this but it doesn't show correctly. If someone could help me with this i would be really thankfull.
<table border="1">
<tr>
<th>month</th>
<th>st_case</th>
<th>veh_id</th>
</tr>
<?php
// Loop on rows in the result set.
$currentmonth = 0;
for($ri = 0; $ri < $numrows; $ri++)
{
$row = pg_fetch_array($result, $ri);
if ($currentmonth != $row["month"])
{
echo "<tr>";
echo "<td>";
echo "<strong>".$row["month"]."</strong>";
echo "</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td>".$row["st_case"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>".$row["veh_id"]."</td>";
echo "</tr>";
$currentmonth = $row["month"];
}
pg_close($link);
?>
</table>
picture: http://i61.tinypic.com/2nb8vgl.png
$ret = pg_fetch_all($result);
$group = array();
foreach($ret as $v){
$group[$v['month']][$v['st_case']][] = $v['veh_id'];
}
foreach($group as $month=>$value){
echo $month."<br/>";
foreach($value as $st_case=>$v){
echo $st_case."<br/>";
foreach($v as $veh_id){
echo $veh_id."<br/>";
}
}
}
PS.then add some css to style your table
If you do not intend to display items in a table, do not use table
What you want is something like this:
<div class="result">
<div class="month_group">
<span class="month bold">1</span>
<div class="case_group">
<span class="case">10001</span>
<div class="veh_group">
<span class="veh_id">1000011</span>
</div>
<span class="case">10002</span>
<div class="veh_group">
<span class="veh_id">1000021</span>
<span class="veh_id">1000022</span>
</div>
</div>
</div>
<div class="month_group">
<span class="month">2</span>
<div class="case_group">
<span class="case">10058</span>
<div class="veh_group">
<span class="veh_id">1000081</span>
</div>
</div>
</div>
</div>
Then all that's left to do is applying simple css to give the classes some padding/margin.
HTML is a markup language. You structure the web page using the HTML tags. However, how it would look like depends more on css (which is why it's called Cascading Style Sheets).
Here's the rendering code. I have not tested it yet, and I have not touched PHP for sometime, but it should give you some general ideas:
echo "<div class=\"result\">";
for($ri = 0; $ri < $numrows; $ri++) {
$row = pg_fetch_array($result, $ri);
$month = $row["month"];
echo "<div class=\"month_group\">\n";
echo "<span class=\"month\">".$month."</span>\n";
echo "<div class=\"case_group\">\n";
for ($ci = $ri; $ci < $numrow; $ci++) {
if ($ci == $ri) {
$c_row = $row;
} else {
$c_row = pg_fetch_array($result, $ci);
}
if ($c_row["month"] != $month) {
// We have moved to another month
break;
}
$case = $c_row["st_case"];
echo "<span class=\"case\">".$case."</span>\n";
echo "<div class=\"veh_group\">\n";
for ($vi = $ci; $vi < $numrow; $vi++) {
if ($vi == $ci) {
$v_row = $c_row;
} else {
$v_row = pg_fetch_array($result, $vi);
}
if ($v_row["st_case"] != $case) {
// We have moved to another case
break;
}
$veh = $v_row["veh_id"];
echo "<span class=\"veh_id\">".$veh."</span>\n";
// we have already processed rows whose indexes are less or equal $vi
$ri = $vi;
}
echo "</div>";
}
echo "</div></div>";
}
Try this updated code
<table border="1">
<tr>
<th>month</th>
<th>st_case</th>
<th>veh_id</th>
</tr>
<?php
// Loop on rows in the result set.
$currentmonth = 0;
for($ri = 0; $ri < $numrows; $ri++)
{
$row = pg_fetch_array($result, $ri);
// Open the row
echo "<tr>";
echo "<td><strong>" . $row["month"] . "</strong></td>"; //open and close each column
echo "<td>".$row["st_case"]."</td>";
echo "<td>".$row["veh_id"]."</td>";
echo "</tr>"; //close the row
$currentmonth = $row["month"];
}
pg_close($link);
?>
</table>
You will also need to call the ORDER BY keyword in your MySQL statement to order by month.
Here is a simple tutorial on how to do this.
Here is a Demo of how it might look in your application.
Let me know if this helps and if i can be of any other help.

Echo out mysql into a html table?

I need some help on how to echo out my mysql data into a html table. I'm trying to put in the relevant table tags where necessary but I must be doing it wrong as its not looking how I want it.
Here's what I am getting:
ID Name Number Note Alert
1
2
Nick Nick insurance insurance alert alert
Here is how I want it to look:
ID Name Number Note Alert
1 Nick Insurance Alert
2 Nick Insurance Alert
Here is my code, can someone please show me where I need to put my table tags to get the desired result:
<?php
include 'config.php';
$data = mysql_query("SELECT * FROM supplier_stats") or die(mysql_error());
echo "<table class=\"table\" style=\"width:900px; font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
font-size:11px; color:#96969;\" >";
while ($info = mysql_fetch_array($data))
{
echo "<tr><td><p>" . $info['id'] . "</p></td></tr>";
}
?>
<?php
include 'config.php';
$data = mysql_query("SELECT * FROM supplier_stats") or die(mysql_error());
while ($info = mysql_fetch_array($data))
{
echo "<td><p>" . $info['company_name'] . "</p></td>";
}
?>
<?php
include 'config.php';
$data = mysql_query("SELECT TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date
FROM supplier_stats") or die(mysql_error()); ?>
<?php
include 'config.php';
$result = mysql_query("SELECT TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date
FROM supplier_stats") or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
$days = $row['expire_date'] - 1;
if ($days > 0)
{
echo "<td><p>Insurance expires in <font color=\"red\">{$row['expire_date']} day(s)!</font></p></td>";
}
else
{
$when = $days * -1;
echo "<td><p>Insurance expires";
if ($when > 1)
{
echo " in {$when} days</p></td>";
}
if ($when >= 8)
{
echo " <div class=\"green_light\"></div>";
}
if ($when <= 7)
{
echo " <div class=\"red_light\"></div>";
}
elseif ($when === 1)
{
echo " tomorrow</p></td>";
}
elseif ($when = 0)
{
echo " today</p></td>";
}
}
}
?>
<?php
include 'config.php';
$result = mysql_query("SELECT TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date
FROM supplier_stats") or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
$days = $row['expire_date'] - 1;
if ($days > 8)
{
echo "a is bigger than b";
}
}
echo "</table>"; //Close the table in HTML
?>
You need more or less somethin like this:
<?php include 'config.php';
$data = mysql_query("SELECT *, TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date FROM supplier_stats")
or die(mysql_error());
echo "<table class=\"table\" style=\"width:900px; font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
font-size:11px; color:#96969;\" >";
while($row = mysql_fetch_array( $data )) {
$days = $row['expire_date'] -1;
echo "<tr><td><p>".$row['id'] . "</p></td>";
echo "<td><p>".$row['company_name'] . "</p></td>";
if ($days > 0) {
echo "<td><p>Insurance expires in <font color=\"red\">{$row['expire_date']} day(s)!</font></p></td>";
} else {
$when = $days*-1;
echo "<td><p>Insurance expires";
if ($when > 1){
echo " in {$when} days</p></td>";
}
if ($when >= 8){
echo " <div class=\"green_light\"></div>";
}
if ($when <= 7){
echo " <div class=\"red_light\"></div>";
} elseif ($when ===1) {
echo " tomorrow</p></td>";
} elseif ($when == 0) {
echo " today</p></td>";
}
}
echo "<tr>";
}
echo "</table>"; //Close the table in HTML
?>
you've closed after first that is why you are getting output like this..remove from:
echo "<tr><td><p>".$info['id'] . "</p></td></tr>";
above line...and close after while loop ends..
<table>
<tr>
<td>Id</td>
<td>Name</td>
<td>Number</td>
<td>Note</td>
<td>Alert</td>
</tr>
<?php
$sql = mysql_query("your query");
$i = 0;
while ($info = mysql_fetch_array($data))
{
$i++
?>
<td><?php echo $i; ?></td>
<td><?php echo $info['name']; ?></td>
<td><?php echo $info['number']; ?></td>
<td><?php echo $info['note']; ?></td>
<td><?php echo $info['alert']; ?></td>
</tr>
<?php
}
?>
</table>

How to create a table in HTML using PHP

I have 5 pictures stored in a folder and their links stored on the database.
I want to put them in a table of three columns on each row.
<body>
<center>
<table border='1'>
<?php
$host="";
$username="";
$password="";
$db_name="fruits_db";
$tbl_name="fruits_tbl";
$connection=mysqli_connect("$host","$username","$password","$db_name");
if (mysqli_connect_errno())
{
echo "The application has failed to connect to the mysql database server: " .mysqli_connect_error();
}
$result = mysqli_query($connection, "SELECT * FROM fruits_tbl")or die("Error: " . mysqli_error($connection));
$num_rows=mysqli_num_rows($result);
$rows = $num_rows/3;
for($i=1; $i<=$rows ; $i++)
{
echo "<tr>";
for($j=1; $j<=3; $j++)
{
while($row = mysqli_fetch_array($result))
{
echo
("<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>"
);
}
}
echo "</tr>";
}
mysqli_close($connection);
?>
</table>
</center>
</body>
</html>
The above code I created, contains two FOR loops. The script should count the number of rows in the table, and then divide by 3(the number of columns on each row in the HTML table).
I wonder where I'm going wrong wit this code.
With your while($row = mysqli_fetch_array($result)){} inside your 1st for loop it will run through all your rows, before the outside loop runs 2nd/3rd time.
Here is another way to do it -
$counter = 1;
// start 1st row
echo "<tr>";
while($row = mysqli_fetch_array($result)){
// if the 4th cell, end last row, and start new row
if ($counter%3==1){
echo "</tr><tr>";
}
echo
"<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>";
// increase the counter
$counter++;
}
// close the last row
echo "</tr>";
You're looping through all the results in the first table cell.
Try something like this instead:
for($i=1; $i<=$rows ; $i++) {
echo "<tr>";
for($j=1; $j<=3; $j++) {
$row = mysqli_fetch_array($result);
if ($row) {
echo(
"<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>"
);
}
}
echo "</tr>";
}
If you just want to format the display with a new row after every 3 records, you could use the modulus operator:
$cntr = 0;
echo '<tr>';
while($row = mysqli_fetch_array($result)) {
$cntr++;
echo '
<td width="180px" height="200px">
<div class="fruit_image">
<img src="'.$row['fruit_image'].'" />
</div>
<div class="fruit_title">'.$row['fruit_name'].'</div>
</td>';
if ($cntr % 3 == 0 && $cntr != $num_rows)
echo '</tr><tr>';
}
echo '</tr>';
Keep in mind however that all the solutions presented so far may leave you with a last row with one or two td elements. You can fill this if you desire with empty <td> </td> columns.
print "<center><table border=1>
<tr>
<td>id</td>
<td>name</td>
<td>company</td>
<td>branch</td>
<td>job title</td>
<td>contact</td>
<td>email</td>
<td>mgs</td>
</tr>";
while($row=mysql_fetch_array($query))
{
print "<tr>";
for ($i=0;$i<=(count($row)/2);$i++)
{
print "<td>$row[$i]</td>";
} print"</tr>";
}
}
else{echo " <p>No Records Found</p>";}
<?php
function studentTable($name,$grade){
echo "<tr> <td>$name</td><td>$grade</td></tr>";
}
?>
<table style="width:100%" border="solid">
<tr>
<th>Name</th>
<th>Grade</th>
</tr>
<?php studentTable("Sarah", 90) ?>

why is this ajax call not reading my internal php?

Too many edits to keep track of, but I have simplified the issue. I have this code in my index.php:
<div class="calendar_top">
<?php
include(SITE_ROOT . "/includes/sub_top_divs.php"); ?>
</div>
<table class="tablebodycontainer"><tr><td>
<?php
include(SITE_ROOT . "/includes/view-monthly-calendar-ajax.php");
?>
</td></tr></table>
<?php include(SITE_ROOT . "/includes/copyright.php"); ?>
</div>
The subtop above contains the navigation links:
includes/subtop-divs.php:
<?php
//include(SITE_ROOT . "/includes/set-variables.php");//dateFormat($date)
echo "<table class='navtabs' cellpadding='0' cellspacing='0'>";
echo "<tr><td class='right'>";
echo buildMenuNavigation($currentPageIndex);
echo '</td></tr></table>';
?>
Here is that buildMenuNavigation function:
function buildMenuNavigation($currentIndex=0) {
$navtabs = array(
'0'=>array('Monthly'=>'index.php'),
'1'=>array('Daily'=>'agenda.php'),
'2'=>array('Admin'=>'admin/view-timelines.php'),
'3'=>array('Help'=>'help.php'),
);
$sep = '<li> | </li>';$builtNav=array();
foreach($navtabs as $index=>$tablinks) {
foreach($tablinks as $key=>$value) {
$class='';
if($index==$currentIndex) {
$class=' class="selected"';
}
//pr($value);
$builtNav[] = '<li><a href="' . SITE_URL . '/' . $value.'"' . $class .'> '.$key.' </a></li>';
}
}
return '<ul>' . implode($sep,$builtNav) . '</ul>';
}
The only data that actually changes when switching pages is the contents in the above:
<?php
include(SITE_ROOT . "/includes/view-monthly-calendar-ajax.php");
?>
So this seems like a perfect candidate for ajax.
Here is the contents of /includes/view-monthly-calendar-ajax.php:
<?php
$counter = 0;
?>
<table class="tabbody">
<tr>
<td class='head unselectable'>Sun</td>
<td class='head unselectable'>Mon</td>
<td class='head unselectable'>Tue</td>
<td class='head unselectable'>Wed</td>
<td class='head unselectable'>Thu</td>
<td class='head unselectable'>Fri</td>
<td class='head unselectable'>Sat</td>
</tr>
<tr>
<?php
//echo $year."-".$month."-";
$flag = 0;
$daysInrow = 0;
for($i = 1; $i < $numDays+1; $i++, $counter++)
{
$daysInrow++;
$zero = "";
if($i < 10)
{
$zero = "0";
}
$t_date = $year."-".$month."-".$zero.$i;
//$t_date = "$year-$month-$i";
$timeStamp = strtotime($t_date);
$eventID = 0;
$eventName = " ";
$bgColor = "";
$funcBG = "setbgcolorMonth($i);";
if($i == 1)
{
// Workout when the first day of the month is
$firstDay = date("w", $timeStamp);
for($j = 0; $j < $firstDay; $j++, $counter++)
echo "<td class='tddaybox'></td>";
}
if($counter % 7 == 0)
{
$daysInrow = 1;
echo "</tr><tr>";
}
if(date("w", $timeStamp) == 0)
if($i == date("d") && $month == date("m") && $year == date("Y"))
$class = "class='today'";
else
$class = "class='weekend'";
else
if($i == date("d") && $month == date("m") && $year == date("Y"))
$class = "class='today'";
else
$class = "class='normal'";
$numric_time = getNumericTime($_SESSION['userData']['timezone']);//Get the numeric timezone
$query = "SELECT * FROM events WHERE date(convert_tz(StartDate,'+00:00','". $numric_time."'))='".$t_date."' AND UserID='" . $_SESSION['userData']['UserID'] ."' ORDER BY PTLType ASC";
//br();
$result = mysql_query($query);
if(mysql_num_rows($result))//cursor:hand;cursor:pointer;width:112px;
{
$funcBG = "";
echo "<td valign='top' class='a_cursor_width tddaybox' id='day$i' onclick='setbgcolorMonth($i)'>";
echo "<div class='td_overlow'>";
echo '<table style="width:100%;border:0;">';
echo '<tr class="rowColor"><td '.$class.'><div class="div_left">'.$i.'</div><div class="div_left_80" onclick="'.$funcBG.' get_event_popup(window.event, this, \''.SITE_URL.'/event-popup.php\', \'eventID='.$eventID.'\', \'date='.$date.'\', \'day='.$i.'\', \'type=M\');return false"> </div></td></tr>';
while($row = mysql_fetch_assoc($result))
{
$eventID = $row['EventID'];
$parentEventID = $row['ParentEventID'];
$eventName = stripslashes($row['EventName']);
$PTLType = $row['PTLType'];
$textclass = "title4";
$onclick_call = 'get_event_popup(window.event, this, \''.SITE_URL.'/event-popup.php\', \'eventID='.$eventID.'\', \'date='.$date.'\', \'day='.$i.'\', \'type=M\')';
if($PTLType != 0) {
$onclick_call = 'get_event_popup(window.event, this, \''.SITE_URL.'/timeline-popup.php\', \'eventID='.$eventID.'\', \'parentEventID='.$parentEventID.'\', \'type=M\')';
$bgColor = "";
$textclass = "redtext";
if($PTLType == 3) {
$textclass = "bluetext";
$display = "none";
} else {
$display = "block";
}
$event_name_wrapped = '<div>'.$eventName.'</div>';
} else {
$textclass = "mainEvent";
$display = "block";
$event_name_wrapped = '<div>'.$eventName.'</div>';
}
echo '<tr><td onclick="'.$onclick_call.';return false;" class="'.$textclass.' a_cursor" title="'.$eventName.'" style="display:'.$display.';">'.$event_name_wrapped.'</td></tr>';
}
echo '<tr><td onclick="get_event_popup(window.event, this, \''.SITE_URL.'/event-popup.php\', \'eventID=0\', \'date='.$date.'\', \'day='.$i.'\', \'type=M\');return false;" class="a_cursor"> </td></tr>';
echo "</table>";
echo "</div>";
echo "</td>";
}
else
{
echo '<td id="day'.$i.'" height="80" valign="top" onclick="'.$funcBG.' get_event_popup(window.event, this, \''.SITE_URL.'/event-popup.php\', \'eventID='.$eventID.'\', \'date='.$date.'\', \'day='.$i.'\', \'type=M\');return false;" class="tddaybox a_cursor">';
echo '<table style="width:100%;">';
echo "<tr class='rowColor'><td $class>$i</td></tr>";
echo "<tr><td> </td></tr>";
echo "</table>";
echo "</td>";
}
}
for($l=0;$l<7-$daysInrow;$l++)
{
echo "<td class='tddaybox'> </td>";
}
?>
</tr>
</table>
<?php
/*
if(isset($divRed))
{
$divRed = implode(",",$divRed);
echo "<div id='divRed1' style='display:none'>$divRed</div>";
}
else
{
echo "<div id='divRed1' style='display:none'>0</div>";
}
if(isset($divBlue))
{
$divBlue= implode(",",$divBlue);
echo "<div id='divBlue1' style='display:none'>$divBlue</div>";
}
else
{
echo "<div id='divBlue1' style='display:none'>0</div>";
}
if(isset($divMainEvent))
{
$divMainEvent= implode(",",$divMainEvent);
echo "<div id='divMainEvent1' style='display:none'>$divMainEvent</div>";
}
else
{
echo "<div id='divMainEvent1' style='display:none'>0</div>";
}
*/
?>
<div id='gig1' style='display:none'>0</div>
<div id='todo1' style='display:none'>0</div>
<div id='completed1' style='display:none'>1</div>
<input type="hidden" id='lastselectedday' value='' />
So the output for all of the above is correct now, but when I try and do this:
<form>
<input type="button" value="Display Message" onclick="getData('/Dev2/includes/view-monthly-calendar-ajax.php', 'targetDiv')">
</form>
<table class="tablebodycontainer">
<tr>
<td id="MainCalendarContainer">
<div class="ajaxswap" id="targetDiv">
</div>
The output of the ajax call doesn't execute the php.
First off, you shouldn't pull <script> elements via Ajax, they are not going to be interpreted and if they are, they are going to break everything on the second request.
Do some debugging by calling /Dev2/includes/view-monthly-calendar-ajax.php in your browser first. Check out the source code of what you are getting. PHP runs before the document is delivered, so there is no way to pull the HTML, but not the contents generated by PHP. They are one entity.
You either have an error in your PHP code that leads to the wrong output, or your PHP code is not being interpreted at all, which is easy to spot because the original PHP source code will be in the document's source code (which must never, ever happen).
The PHP code is processed by the server; the browser will never see it unless the web server glitches.
PHP is preprocessed before the data is sent to the client. The server parses and executes the php code and only sends what is output by the code. If you want to get the full file you would need to have it not processed by php, for instance by renaming the file extension to something else.
You should implement another php page such as...
getData('getPHP.php?page=/Dev2/includes/view-monthly-calendar-ajax.php', 'targetdic');
Then in that page you need to implement a method to load the php file as a string and return it. at the moment the php is being interpreted as you would expect.

Categories