I am trying to create a menu where users can view a menu that they create. The menu consists of a dropdown box which the users use to filter the menu by week.
The menu then displays the seven days of the week and the five types of meal times their are which is breakfast, lunch, dinner, pudding and snack. Below is how i am currentley displaying it so far.
$sqlmeasurement = mysql_query("SELECT title FROM recipe JOIN menu ON recipe.recipeid = menu.recipeid WHERE menu.weekid = '".$selectweek."' AND menu.mealtimeid = '1'") or die(mysql_error());
Print '<table border="0">
<tr>
<th width="100"> </th>
<th width="200">Monday</th>
</tr>';
echo "<tr>";
echo "<td><strong>Breakfast</strong></td>";
echo "<td>";
while($info = mysql_fetch_array( $sqlmeasurement ))
{
echo $info['title'];
echo "<br/>";
}
echo "</td> ";
echo "</tr>";
echo "</table>";
As you can see this PHP statement selects what i want from the table but because there are 7 days a week and 5 types of meal times this means that i will have to copy this code 35 times in order to display all that i want.
So for example the next bit of code will be like this:
$sqlmeasurement2 = mysql_query("SELECT title FROM recipe JOIN menu ON recipe.recipeid = menu.recipeid WHERE menu.weekid = '".$selectweek."' AND menu.mealtimeid = '2'") or die(mysql_error());
Print '<table border="0">';
Print '<tr>
<th width="100"> </th>
<th width="200"> </th>
</tr>';
echo "<tr>";
echo "<td><strong>Lunch</strong></td>";
echo "<td>";
while($info2 = mysql_fetch_array( $sqlmeasurement2 ))
{
echo $info2['title'];
echo "<br/>";
}
echo "</td> ";
echo "</tr>";
echo "</table>";
And so on until i have all the days and their meal times.
Is there a way to display all the information without all this iteration?
You have various options. You could put the output into a function, and just call that with varying parameters.
function ouptutMeal($selectweek, $mealtime, $mealname) {
$sqlmeasurement2 = mysql_query("SELECT ... WHERE menu.weekid = '$selectweek' AND menu.mealtimeid = '$mealtime'");
echo "<table>
<tr>
<th> </th>
<th> </th>
</tr>
<tr>
<td><strong>$mealname</strong></td>
<td>";
while($info2 = mysql_fetch_array( $sqlmeasurement2 )) {
echo $info2['title'], '<br/>';
}
echo '</td>
</tr>
</table>';
}
ouptutMeal($selectweek, 1, 'breakfast');
ouptutMeal($selectweek, 2, 'lunch');
Or you could use a different query to get all the information at once, and loop through it:
$q = "SELECT title, mealtimeid, mealtimename
FROM ...
WHERE menu.weekid = '$selectweek'
ORDER BY menu.mealtimeid";
You can then iterate through the results, assembling them into, say, a 2D array based on using mealtimename as a key. e.g.
$mealInfo = array()
while($info = mysql_fetch_array( $result )) {
$mealInfo[$info['mealtimename'][] = $info;
}
foreach ($mealInfo as $mealName => $mealData) {
ouptutMeal($mealName, $mealData);
}
Related
i have a website project, where customers will need to search and the results will display, so i have done all that, but the problem is when a search results comes up,if there's more than 1 row,it dublicate the results on the same row,here are the code.
<h1 style="font-family:calibri;color:#13CC0D;text-align:center;">BODABODA SEARCH RESULTS</h1>
<table cellpadding="1" cellspacing="1" id="resultTable">
<thead>
<tr>
<th style="border-left: 1px solid #C1DAD7"> Region</th>
<th> District </th>
<th> Ward </th>
<th> Street</th>
<th> Driver Name </th>
<th>Identification Type</th>
<th>Identification Number</th>
<th>Motorcycle Type</th>
<th>Motorcycle Reg No</th>
<th>Phone Number</th>
</tr>
</thead>
<?php
$conn = mysqli_connect('localhost', 'efalococ_calcia', '*ad4#zNQ=nfN') or die('can not connect to the server'.mysqli_error);
mysqli_select_db($conn, 'efalococ_safirii');
if(isset($_POST['search'])){
$query = $_POST['region'];
$query1 = $_POST['district'];
$query2 = $_POST['ward'];
$query3 = $_POST['street'];
$results = mysqli_query($conn,"SELECT * FROM bodaboda WHERE (`region` LIKE '%".$query."%') && (`district` LIKE '%".$query1."%') && (`ward` LIKE '%".$query2."%') && (`street` LIKE '%".$query3."%')") or die(mysql_error());
if(mysqli_num_rows($results) >0){
while($row = mysqli_fetch_array($results)){
echo "<td>".$row['Region']."</td>" ;
echo "<td>".$row['District']."</td>";
echo "<td>".$row['Ward']."</td>";
echo "<td>".$row['Street']."</td>";
echo "<td>".$row['DriverName']."</td>";
echo "<td>".$row['IdentificationType']."</td>";
echo "<td>".$row['IdentificationNumber']."</td>";
echo "<td>".$row['MotorcycleType']."</td>";
echo "<td>".$row['MotorcycleRegNo']."</td>";
echo "<td>".$row['PhoneNumber']."</td>";
}
}else{
echo '<span style="color:red;font-family:cursive;font-size:14px;">No results found, Please Modify your search </span> Click here'.mysqli_error($conn);
}
}
?>
</table>
And the results shows like this!
Can you please help on that? any idea?
You just need to use the <tr> for every row.
The tag defines a row in an HTML table.
Learn more about HTML tr tag
while($row = mysqli_fetch_array($results)) {
echo "<tr>";
echo "<td>".$row['Region']."</td>" ;
echo "<td>".$row['District']."</td>";
echo "<td>".$row['Ward']."</td>";
echo "<td>".$row['Street']."</td>";
echo "<td>".$row['DriverName']."</td>";
echo "<td>".$row['IdentificationType']."</td>";
echo "<td>".$row['IdentificationNumber']."</td>";
echo "<td>".$row['MotorcycleType']."</td>";
echo "<td>".$row['MotorcycleRegNo']."</td>";
echo "<td>".$row['PhoneNumber']."</td>";
echo "</tr>";
}
As I'm new to PHP, I want to know that how to put data from different database tables into one table form on the page.
My codes so far as below,
<?php
include('DBconnect.php');
mysql_query("USE onlinerecruitment");
$username =$_SESSION['user'];
$result = mysql_query("SELECT * FROM application_data_file");
$rows = mysql_fetch_array($result, MYSQL_ASSOC);
$pos_id = $rows['Position_ID'];
$resultt = mysql_query("SELECT * FROM position WHERE Position_ID = '".$pos_id."' ");
$resulttt = mysql_query("SELECT * FROM resume_data_file WHERE App_Email = '".$pos_id."' ");
?>
<TABLE border ='1'>
<table style="width:100%">
<tr>
<th>Application ID</th>
<th>Applicant E-mail</th>
<th>Position Selected</th>
<th></th>
<th></th>
<th></th>
</tr>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) & $rowss = mysql_fetch_array($resultt, MYSQL_ASSOC)){
echo "<TR>";
echo "<TD>".$row['App_Data_ID']."</TD>";
echo "<TD>".$row['App_Email']."</TD>";
echo "<TD>".$rowss['Position_Name']."</TD>";
echo "<TD><a href='view-app-form.php?app_mail=".$row['App_Email']."'>View Application Data</a></TD>";
echo "<TD><a href='view-resume-form.php?app_mail=".$row['App_Email']."'>View Resume Data</a></TD>";
echo "<TD><a href='view-test-score.php?app_mail=".$row['App_Email']."'>View Testing Score Data</a></TD>";
echo "</TR>";
}
?>
</table>
I will focus the part here.
<TABLE border ='1'>
<table style="width:100%">
<tr>
<th>Application ID</th>
<th>Applicant E-mail</th>
<th>Position Selected</th>
<th></th>
<th></th>
<th></th>
</tr>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) & $rowss = mysql_fetch_array($resultt, MYSQL_ASSOC)){
echo "<TR>";
echo "<TD>".$row['App_Data_ID']."</TD>";
echo "<TD>".$row['App_Email']."</TD>";
echo "<TD>".$rowss['Position_Name']."</TD>";
echo "<TD><a href='view-app-form.php?app_mail=".$row['App_Email']."'>View Application Data</a></TD>";
echo "<TD><a href='view-resume-form.php?app_mail=".$row['App_Email']."'>View Resume Data</a></TD>";
echo "<TD><a href='view-test-score.php?app_mail=".$row['App_Email']."'>View Testing Score Data</a></TD>";
echo "</TR>";
}
?>
</table>
But if there is any problem in the section that I didn't focused, I still appreciate your solution.
Thank you in advance.
To do this you would need to use a JOIN in the sql statement.
mysql_query("SELECT resume_data_file.App_Email, position.Position_ID FROM position INNER JOIN resume_data_file ON position.Position_ID = position.Position_ID WHERE position.Position_ID = '".$pos_id."' ");
http://www.w3schools.com/sql/sql_join.asp
After I printed those values by using PHP, I got confused how to get the value of a selected cell.
<div>
<php
$query = mysql_query(select * from items, $conn);
echo "<table border='1'>
<th width ='120'>itemID</th>
<th width ='120'>itemName</th>";
while(row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>".$row['itemID']."</td>";
echo "<td>".$row['itemName']."</td>";
}
echo "</table>";
?>
</div>
seems you doing fine, iv'e just edited some typos, added tags,and changed to: "mysql_fetch_assoc" function. try this:
<div>
<?php
$query = mysql_query("select * from items", $conn);
echo "<table border='1'>
<tr>
<th width ='120'>itemID</th>
<th width ='120'>itemName</th>
</tr>";
while($row = mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>".$row['itemID']."</td>";
echo "<td>".$row['itemName']."</td>";
echo "</tr>";
}
echo "</table>";
?>
</div>
I've a mysql table named product with the following structure:
CREATE TABLE product (
id int(6) unsigned NOT NULL auto_increment,
name varchar(100) NOT NULL default '',
image varchar(255) NOT NULL default '',
link varchar(255) NOT NULL default '',
price decimal(3,2) NOT NULL default '0.00',
PRIMARY KEY (id)
);
and I would like to visualize the retrieved results {product1,...,product5} into a html/php table of 3 columns with also the following structure:
<table border="1" cellpadding="2" cellspacing="2" width="100%">
<tr>
<td>product1.image</td> <td>product2.image</td> <td>product3.image</td>
</tr>
<tr>
<td>product1.name</td> <td>product2.name</td> <td>product3.name</td>
</tr>
<tr>
<td>product1.link</td> <td>product2.link</td> <td>product3.link</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td>product4.image</td> <td>product5.image</td> <td> </td>
</tr>
<tr>
<td>product4.name</td> <td>product5.name</td> <td> </td>
</tr>
<tr>
<td>product4.link</td> <td>product5.link</td> <td> </td>
</tr>
</table>
Note that the fields of a given entry (e.g. product1.{image,description,link}) are displayed in different rows.
The code below illustrates how do the images of some products could be displayed.
$table = 'product';
$result = mysql_query("SELECT id, image, description, link FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='1' cellpadding='2' cellspacing='2' width='100%'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td> <img src="$field->image" border="0"></td>";
if($i%3==0){echo"</tr><tr>";}
}
echo "</tr></table>";
Any suggestions are appreciated!
So you want an html structure, which always displays three products next to each other, and for each product places the image above the description which is above the link.
... and I wouldn't use a table for it.
I'm assuming you know how to fetch data from a database.
You loop through the result table and the display as you need it:
$counter = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<span><div>".$row["image"]."</div><div>".$row["description"]."</div><div>".$row["link"]."</div></span>;
$counter += 1;
if($counter % 3 = 0)
{
echo "</br>
}
}
This gives you a set of 3 elements next to each other, and within them 3 div elements above each other, each containing a detail about your product.
Here's the code for using a surrounding table:
echo "<h1>Table: {$table}</h1>";
echo "<table border='1' cellpadding='2' cellspacing='2' width='100%'><tr>";
$counter = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<td><div>".$row["image"]."</div><div>".$row["description"]."</div><div>".$row["link"]."</div></td>;
$counter += 1;
if($counter % 3 = 0)
{
$echo "</tr><tr>";
}
}
echo "</tr></table>";
... and here's as you requested in your question:
echo "<h1>Table: {$table}</h1>";
echo "<table border='1' cellpadding='2' cellspacing='2' width='100%'>";
$counter = 1;
$htmlHolder1 = "<tr>";
$htmlHolder2 = "<tr>";
$htmlHolder3 = "<tr>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$htmlHolder1 .= "<td>".$row["image"]."</td>";
$htmlHolder2 .= "<td>".$row["description"]."</td>";
$htmlHolder3 .= "<td>".$row["link"]."</td>";
$counter += 1;
if($counter % 3 = 0)
{
echo $htmlHolder1."</tr>";
echo $htmlHolder2."</tr>";
echo $htmlHolder3."</tr>";
$htmlHolder1 = "<tr>";
$htmlHolder2 = "<tr>";
$htmlHolder3 = "<tr>";
}
}
if($counter % 3 != 0)
{
echo $htmlHolder1."</tr>";
echo $htmlHolder2."</tr>";
echo $htmlHolder3."</tr>";
}
echo "</table>";
A loop to build the cells, counting each one as you go, then every XX (third using your example) you close the row and open a new one.
Not sure if you need more or not but that is the basics.
The code below isn't working as intended. I was wanting 3 columns with the first column being a site name, the second column being the category, and the third being the URL for RSS (haven't gotten to that part yet). The data is in the database but when I view the result of the statements below, the site name results link to the current web page I am on. I would greatly appreciate any assistance with getting each column of data to display on the page correctly. Thank you
<?php
$query="SELECT * FROM SOMETABLE";
$result=mysql_query($query);
$num = mysql_numrows($result);
echo "
<table border='1'>
<th>Site Name:</th>
<th>Category:</th>
<th>RSS:</th>";
$i=0;
while ($i < $num) {
$siteName =mysql_result($result,$i,"siteName");
$category =mysql_result($result,$i,"category");
$category =mysql_result($result,$i,"url");
$rss =mysql_result($result,$i,"rss");
echo "
<tr>
<td><a href='$url'>$siteName</a></td>
<td>$category</td>
<td>$rss</td>
</tr>";
$i++;
}
?>
echo "<table>";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr>";
echo "<td>$line ["siteName"]</td>";
echo "<td>$line ["category"]</td>";
echo "<td>$line ["url"]</td>";
echo "<td>$line ["rss"]</td>";
echo "</tr>";
}
echo "</table>";
skip $num, mysql_resuls ( the way how you do it ) open row TR in headers ( I skipped it ) and close table
echo "
<table border='1'>
<th>Site Name:</th>
<th>Category:</th>
<th>RSS:</th>";
Should be:
echo "
<table border='1'>
<tr>
<th>Site Name:</th>
<th>Category:</th>
<th>RSS:</th>
</tr>";
To start with.
Or just use some tool like SDTable.com or jqGrid and let them do all job for you =)