Pulling in data from MySQL and breaking it up between pages - php

I have working code that brings in data from my database as follows:
require("database.php");
$result = mysqli_query($con,"SELECT * FROM menuitem");
echo "<table width='1024' border='0' cellpadding='10' cellspacing='5' align='center'>
<tr>
<th></th>
<th>Menu Items</th>
<th>Description</th>
<th>Price</th>
<th>Add to Order</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td align='center'><img src=\"" . $row['picturepath'] . "\" /></td>";
echo "<td align='center'>" . $row['name'] . "</td> <td align='center'> <input type='button' value='More Info'; onclick=\"window.location='more_info.php?';\"> </td>";
echo "<td align='center'>" . $row['price'] . "</td> <td align='center'> <input type='button' value='Add to Order' onclick=''> </td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
However, the columns that I am calling have over 20 items. I want to limit the amount of items the page displays to 4 items per page, and then create multiple pages listing the remaining items in the database.
So far I have tried adding in the if then statement, but that didn't work very well. I'm not sure if that is even the correct way to go about it.

SELECT * FROM menuitem LIMIT $startPositionVariable,$howManyItemsPerPageVariable
a separate query should determine how many total entries there are to begin with and determine how many pages accordingly.

Related

mysql query not formatting correctly

I am very close on this but still having a problem with the listing of events. What I am looking for is for the title, date and event to show once with a list of names who participated in that event. Now I am getting an event, date and title printed as many times as names, with only one name showing in each.
I have tried GROUP BY on several fields but nothing seems to work.
SELECT x.xdate, x.xevent, x.xname, x.xaffirm, y.activity, y.title,
y.text, y.date
FROM x LEFT JOIN y ON x.xdate = y.date
WHERE xevent='Hiking' AND xaffirm='Yes'
ORDER BY y.date DESC")
Print "<table width=500>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print
"<th style='width:225px;' bgcolor=#49A78D align=center >Title</th>
<th style='width:300px;' bgcolor=#49A78D align=center >Text</th>
<th style='width:175px;' bgcolor=#49A78D align=center >Date</th>
<th style='width:175px;' bgcolor=#49A78D align=center >Name</th>";
Print "</tr>";
Print "<tr>";
Print
"<td bgcolor=#ffffff valign=top align=center>".$info['title'] . "</td>
<td bgcolor=#ffffff valign=top align=left>".$info['text'] . "</td>
<td bgcolor=#ffffff valign=top align=left>".$info['date'] . "</td>
<td bgcolor=#ffffff valign=top align=left>".$info['name'] . "</td>";
Print "</tr>";
This is what I am getting now:undesired
This is what I would like to get: desired
What I'd probably do is to have a single variable that denotes "does the data in this row correspond to a new event, or is it additional data for the same event?"
If the date doesn't match, it's a new event; first, add a new header row, then add a new row with data for all four columns. If the date does match, then skip the header and add a row where the first three columns are empty and the fourth has the new name. (Apologies if my PHP doesn't quite compile, it's been a while.)
$lastEventDate = '';
Print "<table width=500>";
while($info = mysql_fetch_array( $data ))
{
if ($lastEventDate != $info['date'])
{
Print "<tr>";
Print
"<th style='width:225px;' bgcolor=#49A78D align=center >Title</th>
<th style='width:300px;' bgcolor=#49A78D align=center >Text</th>
<th style='width:175px;' bgcolor=#49A78D align=center >Date</th>
<th style='width:175px;' bgcolor=#49A78D align=center >Name</th>";
Print "</tr>";
}
Print "<tr>";
if ($lastEventDate == $info['date'])
{
Print "<td colspan=3 />"
}
else
{
Print
"<td bgcolor=#ffffff valign=top align=center>".$info['title'] . "</td>
<td bgcolor=#ffffff valign=top align=left>".$info['text'] . "</td>
<td bgcolor=#ffffff valign=top align=left>".$info['date'] . "</td>"
}
Print "<td bgcolor=#ffffff valign=top align=left>".$info['name'] . "</td>";
Print "</tr>";
$lastEventDate = $info['date'];
}
Also, I'm unsure whether what you actually want is separate tables; if so, then inside the first if block, you can close the first table, then open a new one.

MySQL UPDATE statement is not updating database data through web form

Scenario: I have a web form where the user can manually enter data (see below). The user will submit the form and the data will be automatically added to the database.
date, order_ref, first_name, last_name, postcode, country, quantity, scott_packing, packing_price, courier_price, dispatch_type, job_status
On another page, the user will be able to only view all the jobs that are currently being (this data is taken from the database) processed and add the tracking number and edit the packing_price,courier_price and job_status and submit the new data.
http://i754.photobucket.com/albums/xx182/rache_R/Screenshot2014-04-23at104045_zps2a628d50.png
Issue: When the user clicks the 'submit all' button, the user is supposed to be redirected to the thank you page which simply notifies the user that their entry has been successful however at the moment, the user is only directed to a blank page which contains the navigation menu. I have checked the database to see if the data has been updated but nothing has changed. How do i get my update statement to work so that the user can update the existing jobs?
This is the code for the page that displays all the jobs:
<?
session_start();
if(!session_is_registered(myusername))
{
header("location:../index.php");
}
include("../template/header.php");
include("../controllers/cn.php");
$sql = "SELECT * FROM Jobs";
$qry = mysql_query($sql);
echo "<div class='content'>";
echo "<form class='form_edit' method='post' action='updatejob.php'>";
echo "<table id='job_list' cellpadding='0' cellspacing='0'>
<tr>
<th>Job No</th>
<th>Date</th>
<th>Qty</th>
<th>Postcode</th>
<th>Country</th>
<th>Packed by Scott</th>
<th>Packing Price</th>
<th>Courier Price</th>
<th>Tracking No</th>
<th>Dispatch Type</th>
<th>Job Status</th>
</tr>";
while($row = mysql_fetch_array($qry))
{
echo "<tr>";
echo "<td width='80' style='text-align: center;'>" . $row['order_ref'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['date'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['quantity'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['postcode'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['country'] . "</td>";
echo "<td width='100' style='text-align: center;'><input type='CHECKBOX' id='scott_packing' name='scott_packing' value='". $row['scott_packing'] . "'></td>";
echo "<td width='100' style='text-align: center;'><input type='text' id='packing_price' name='packing_price' value='". $row['packing_price'] . "'/></td>";
echo "<td width='100' style='text-align: center;'><input type='text' id='courier_price' name='courier_price' value='". $row['courier_price']."'/></td>";
echo "<td width='100' style='text-align: center;'><input type='text' id='tracking_number' name='tracking_number' value='". $row['tracking_number'] . "'/></td>";
echo "<td width='100' style='text-align: center;'>" . $row['dispatch_type'] . "</td>";
echo "<td width='100' style='text-align: center;'><select name='job_status' id='job_status'>
<option value='". $row['job_status'] ."'>". $row['job_status']. " <option value='dispatched'>Dispatched</td>";
//echo "<td width='100' style='text-align: center;'><a href='editjob.php'>edit</td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' name='submit' value='submit all'/>";
echo "</form>";
mysql_close();
?>
And here is the code that is supposed to update the data:
<?
session_start();
if(!session_is_registered(myusername)){
header("location:../index.php");
}
include("../template/header.php");
include("../controllers/cn.php");
if (isset($_POST['submit']))
{
$order_ref = $_POST['order_ref'];
$packing_price = $_POST['packing_price'];
$courier_price = $_POST['courier_price'];
$tracking_number = $_POST['tracking_number'];
$job_status = $_POST['job_status'];
$sql_qry = "UPDATE Jobs SET '$packing_price, $courier_price, $tracking_number, $job_status' WHERE order_ref = '$order_ref'";
$query = mysql_query($sql_query);
if(!$query)
{
die('Could not update data' .mysql_error());
} else
{
header("location: updatesuccess.php");
exit;
}
mysql_close();
}
?>
Your update query is wrong. The query of update should be like
UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]
In this case it will be something like
UPDATE Jobs SET packing_price='$packing_price',courier_price='$courier_price',tracking_number='$tracking_number',job_status='job_status' WHERE order_ref = '$order_ref'";
and also you missed out the name property for input fields. If you don't specify it you can't access it like $_POST['packing_price'] where packing_price is be the name of the input field.
Also add method="post" to the form like
echo "<form class='form_edit' action='updatejob.php' method='post'>";
Try with this query
$sql_qry = "UPDATE Jobs SET column1 = '$packing_price', column2 ='$courier_price', .... WHERE order_ref = '$order_ref'";
And like Roland Jansen stated you are missing the name attributes on your input tags

Pulling data into a second script using a "more info" button from first script

I'm not entirely sure I'm even asking this correctly. I have a lack of terminology, and I apologize for that up front. You see, I have two php scripts, and I'm trying to learn how php and MySQL work together. In my first php script, order.php, I pull information from my database and display it on the page. Under each item that is pulled, there is a "more info" button. My goal is to have a user click on "more info" for any particular item, and when they do, be redirected to a new page that lists THAT items information.
So far, I have the "more info" button linking to a moreinfo.php script, and it is retrieving THAT items description using urlencode, and displaying it on moreinfo.php.
What I need, is to have the "more info" button on "order.php" pull THAT items description, name, and price.
I've tried adding:
. urlencode($row['description']) . urlencode($row['price']) .
to the code below, and it pulls in the information, but I can't separate it. Here is my code for everything so far.
require("database.php"); //connect to the database
$start = (isset($_GET['start']) ? (int)$_GET['start'] : 0); //setting the get function to a variable so I can display data on same page
$result = mysqli_query($con,"SELECT * FROM menuitem LIMIT $start, 3");
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
echo "<table width='1024' border='0' cellpadding='10' cellspacing='5' align='center'>
<tr align='center'>
<th></th>
<th>Menu Items</th>
<th>Description</th>
<th>Price</th>
<th>Add to Order</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td align='center'><img height='100' width='100' src=\"" . $row['picturepath'] . "\" /></td>";
echo '<td align="center">' . $row['name'] . '</td>';
/*echo '<td align="center"><input type="button" value="More Info" onclick="window.location=\'more_info.php?start=' . urlencode($row['description']) .' \';" /></td>';*/
echo '<td align="center"><input type="button" value="More Info" onclick="window.location=\'more_info.php?start=' . urlencode($row['description']) . urlencode($row['price']) .' \';" /></td>';
echo "<td align='center'>" . $row['price'] . "</td> <td align='center'> <input type='button' value='Add to Order' onclick=''> </td>";
echo "</tr>";
}
echo "</table>";
Currently, it is pulling the description and price together, and I have no idea how to separate into different tables. If anyone can even give me a keyword to google, I would gladly look up the info on my own, or do whatever I can to research it to get it working.
Here is the moreinfo.php
$start = (!empty($_GET['start']) ? $_GET['start'] : false);
<html><table width='100%' border='0'><tr><td height="200"></td></tr></table></html>
<?php
echo "<table width='90%' border='0' cellpadding='10' cellspacing='5' align='center'>"
echo "<tr align='center'>
<td width='60%' align='left'>" . $start . "</td>
<td width='40%' align='left'>" "</td>
</tr>";
echo "</table>";
?>
Look at how other sites do the same thing. IE StackOverflow. On the main page, there is a list of questions. When you click a question, that question appears and all data associated with it.
The link to this particular question is http://stackoverflow.com/questions/20622290.
The number at the end of the URL is this questions ID in the database, or some numerical representation of this question anyways.
I would suggest adding the ID of your item to the end of your URLs. IE, http://example.com/moreinfo.php?id=5.
Where 5 is the ID of that item you want to see. This will allow you to grab that items information from the database and you will be able to display it.

MySQL & PHP: Using an INPUT BUTTON to redirect to new page of data

I have some code within my php script that connects to my database, and then displays a list of food items. It works pretty well, but now I need to create a button that links to another page for each specific item in the table. I know this is wrong, but I'm trying to learn so that's why I'm here.
require("database.php");
$result = mysqli_query($con,"SELECT * FROM entrees_menu_table");
echo "<table width='1024' border='0' cellpadding='10' cellspacing='5' align='center'>
<tr>
<th></th>
<th>Menu Items</th>
<th>Description</th>
<th>Price</th>
<th>Add to Order</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td align='center'><img src='./img/burger.png'></td>";
echo "<td align='center'>" . $row['entrees_item'] . "</td> <td align='center'> <input type='button' value='More Info' onclick='location.href('http://www.gmail.com');'> </td>";
echo "<td align='center'>" . $row['entrees_price'] . "</td> <td align='center'> <input type='button' value='Add to Order' onclick=''> </td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
In the above code, the MORE INFO input button needs to redirect to a new page, and display the entree_item_description from my database for each entrees_item. Would I have to completely re-do my entire code to make this happen? I'm lost so far, and was trying to test out onclick with a location, but it's not working at all.

show checkbox data mysql

I am having some minor errors and was wondering if anyone could help!
I am creating a attendance system for a college.
This Scenario:
Student logs in successfully and then wants to view his/her attendance for a particular course.
Problem: I want it to show me both checked and uncheked data from mysql, it currently shows an empty checkbox (when its meant to be checked) also at the moment it is showing numerous duplicated data, is it possible i could limit that, say for example show one record per week , it shows all data and duplicates it.
<?php
$q3= "SELECT attendance.week_number_id, attendance.week_number, courses.course_id, students.student_id, course_attendance.present, course_attendance.notes
FROM courses, course_attendance, attendance, students
WHERE course_attendance.student_id= ".$_SESSION['student_id']." AND course_attendance.course_id= courses.course_id AND course_attendance.week_id= attendance.week_number_id AND courses.course_id='101'
";
$result = mysql_query($q3) or die(mysql_error());
echo "<table border='1' align='center'><tr> <th><strong>Week Number</strong></th> <th><strong>Present</strong></th> <th><strong>Notes</strong></th> </tr> ";
while($row = mysql_fetch_assoc($result))
{
extract($row);
echo
"</td><td width='200' align='center'>" .$row['week_number'].
"</td><td width='400' align='center'><input type='checkbox' name='present'" .$row['present'].
"</td><td width='400' align='center'>" .$row['notes'].
"</td><tr>";
}
echo "</table>";
?>
Note: I am connected successfully to database, mysql is up and running, i am using sessions, currently it does show data for the student but does not show the existing checked or uncheked value, the checkbox is empty.
Can anyone help
In your code, you're not properly defining the checkbox to be checked. Make a code that adds checked="true" if the 'present' field is 1.
<?php
$q3 = " SELECT attendance.week_number_id, attendance.week_number, courses.course_id, students.student_id, course_attendance.present, course_attendance.notes
FROM courses, course_attendance, attendance, students
WHERE course_attendance.student_id= ".$_SESSION['student_id']." AND course_attendance.course_id= courses.course_id AND course_attendance.week_id= attendance.week_number_id AND courses.course_id='101'";
$result = mysql_query($q3) or die(mysql_error());
echo "
<table border='1' align='center'>
<tr>
<th><strong>Week Number</strong></th>
<th><strong>Present</strong></th>
<th><strong>Notes</strong></th>
</tr>
";
while($row = mysql_fetch_assoc($result)) {
$checked = '';
if($row['present'] == 1) {
$checked = ' checked="true"';
}
echo "
<tr>
<td width='200' align='center'>" . $row['week_number'] . "</td>
<td width='400' align='center'>
<input type='checkbox' name='present'" .$checked . "/>
</td>
<td width='400' align='center'>" . $row['notes'] . "</td>
</tr>
";
}
echo "</table>";
?>
Your
echo
"</td><td width='200' align='center'>" .$row['week_number'].
"</td><td width='400' align='center'><input type='checkbox' name='present'" .$row['present'].
"</td><td width='400' align='center'>" .$row['notes'].
"</td><tr>";
statement should be
$present = "";
if(.$row['present']==1)
{
$present = "checked =checked/>";
}
else
{
$present = "/>";
}
echo
"" .$row['week_number'].
"" .$row['notes'].
"";
Hope this helps you. Thanks
$checked = '';
if($present == 1) {
$checked = 'checked="checked"';
}
echo "</td><td width='200' align='center'>" .$row['week_number'].
"</td><td width='400' align='center'><input type='checkbox' name='present'" .$checked . "/>" .
"</td><td width='400' align='center'>" .$row['notes'].
"</td><tr>";
}
echo "</table>";
Try.

Categories