Two Table headings printed on webpage - php

I have to following problem, after running script below it prints out an extra header at the end?
What I am doing is getting data from two tables, with certain parameters then placing them into a table. The echo $result at the end is just to see what is still inside $result and it prints out a value before the extra header as well as right after the header.
How can I get around that?
extract(shortcode_atts(array(
"countryid"=>'',
), $atts));
// Setting up variables
// Get all the data from the "example" table
$joburl = "http://www.x.co.za/job/view/";
$result = mysql_query("SELECT wpjb_job.company_name , wpjb_job.job_category , wpjb_job.job_country ,
wpjb_job.job_title , wpjb_job.job_slug ,
wpjb_category.id , wpjb_category.title
FROM wpjb_job
INNER JOIN wpjb_category ON wpjb_job.job_category = wpjb_category.id
WHERE job_country='$countryid'
AND(is_filled='0' AND is_active='1')
ORDER BY job_title")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Job</th> <th>Company</th> <th>Industry</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo ' '.$row['job_title'].' ';
echo "</td><td>";
echo $row['company_name'];
echo "</td><td>";
echo $row['title'];
echo "</td></tr>";
}
echo "</table>";
echo $result;
}

Ok Try as below
Put your header line and close tag of table into below condition
$count = mysql_num_rows($result);
if($count > 0){
echo "<table border='1'>";
echo "<tr> <th>Job</th> <th>Company</th> <th>Industry</th> </tr>";
}
//your while loop
while(...) {
....
}
if($count > 0){
echo "</table>";
}

Related

Missing the first row of a SELECT statment

I have a database table called 'Modules' and I am trying to select all of the rows from that table and display them in a table. Each row has a column called MOrder which ranges from 1 - how ever many modules are available.
Here is my code:
$sql_query = "SELECT * FROM Modules WHERE CourseID = ". $CourseID ." ORDER BY MOrder ASC";
$result = mysqli_query($dbconfig, $sql_query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
echo '<div class="row">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if($count > 0) {
$_SESSION['countMOrder'] = $count;
echo '<tr>';
echo '<th>Module Title</th> ';
echo '<th></th>';
echo '</tr>';
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>'. $row['Title'] .'</td> ';
echo '<td>Take Module</td>';
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
echo '</div>';
?>
However, for whatever reason the statement is missing out the module with MOrder 1 and always starting with 2?
Why is this?
You are calling $row = mysqli_fetch_array($result, MYSQLI_ASSOC); in the third line of your pasted code, which is pulling the first array from the results.
This is then being overwritten in your while loop:
while ($row = mysqli_fetch_array($result)) { // <-- overwritten here with item 2
//...
}
Because in the 3rd line of your code you call
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
once, and then in the loop you start calling the
$row = mysqli_fetch_array($result)
again, thus overwriting the $row variable with the 2nd row. Get rid of the 1st $row = mysqli_fetch_array($result) line.
Try this code.
$sql_query = "SELECT * FROM Modules WHERE CourseID = ". $CourseID ." ORDER BY MOrder ASC";
$result = mysqli_query($dbconfig, $sql_query);
$count = mysqli_num_rows($result);
echo '<div class="row">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if($count > 0) {
$_SESSION['countMOrder'] = $count;
echo '<tr>';
echo '<th>Module Title</th> ';
echo '<th></th>';
echo '</tr>';
while ($row = mysqli_fetch_array($result)) { //for every fetch we'll get one row.
echo '<tr>';
echo '<td>'. $row['Title'] .'</td> ';
echo '<td>Take Module</td>';
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
echo '</div>';
At the beginning, you fetch the first row.
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
In while loop, fetch function returns second row.

Create multi-row tables in PHP using MySQL

I'm trying to create something like the following:
But my current code has given me:
The part in white is what I get with the code below, and the part in black is what I get when I add in two more <td> rows. The problem is I can't get 3 pieces of data per row to be created - only vertically or horizontally.
$result = mysqli_query($con,"SELECT * FROM show_listing");
while($row = mysqli_fetch_array($result))
{
echo "<table>
<tr>
<td>".$row['name']."'</td>
</tr>
</table>";
}
mysqli_close($con);
It may be simpler than I think but does anyone know how I could do this?
$c = 0;
echo "<table>";
while($row = mysqli_fetch_array($result))
{
if ($c%3 == 0) {
if ($c) echo "</tr>";
echo "<tr>";
}
echo "<td>".$row['name']."</td>";
$c++;
}
if ($c) echo "</tr>";
echo "</table>";
You have the correct idea. You do not want the declaration in the while loop as the result is that you will get multiple tables.
So I moved the table declarations outside of the while loop. I also made the declarations conditional based on the current column. You can now set the variable $columns to however many columns you want.
$result = mysqli_query($con,"SELECT * FROM show_listing");
echo "<table>"
$columns=3;
$current_column=1;
while($row = mysqli_fetch_array($result))
{
if($current_column==1) {
echo "<tr>";
}
echo "<td>".$row['name']."'</td>";
if($current_column==$columns) {
echo "</tr>";
$current_column=1;
} else
$current_column++;
}
echo "</table>
mysqli_close($con);

$row how to get all data into vars

I am building an page the script is done it lists Name and Points in a table. But when i gonna do the design it will be to hard to do it with "echo". So i am woundering if i can get this into vars insted that i can just use in a html file.
Table look like this
Name | Points | Date
What i want is to make a var for the 10 first name rows and a var for the 10 first point rows.
Like
$top1n = $row[0]'name'
$top1p = $row[0]'points'
$top2n = $row[1]'name'
$top2p = $row[1]'points'
$top3n = $row[2]'name'
$top3p = $row[2]'points'
$top4n = $row[3]'name'
$top4p = $row[3]'points'
And so on...
etc.
See my script below
echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Tokens</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $resultbtm )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['Name'];
echo "</td><td>";
echo $row['points'];
echo "</td></tr>";
}
echo "</table>";
Is this what you want?
$names = array();
$points = array();
while($row = mysql_fetch_array($resultbtm)) {
$names[] = $row['Name'];
$points[] = $row['tokens'];
}
echo '<pre>';
print_r($names);
print_r($points);
echo '</pre>';
Not 100% sure if this is what you were looking for, but the $names and $points variables will be arrays containing the Names and Tokens (respectively) from your mysql result set.
Side note: mysql_* functions are deprecated as of PHP 5.5.0 due to security issues. If you are able to it is strongly recommended you switch to mysqli_* or PDO.
You can do it like this: (no need to create so many variables)
$str = "<table border='1'>";
$str .= "<tr> <th>Name</th> <th>Tokens</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $resultbtm )) {
// Print out the contents of each row into a table
$str .= "<tr>";
$str .= "<td>".$row['Name']."</td><td>".$row['points']."</td>";
$str .= "</tr>";
}
$str .= "</table>";
echo $str;
You could use extract
echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Tokens</th> </tr>";
// keeps getting the next row until there are no more to get
$c = 1;
while($row = mysql_fetch_array( $resultbtm )) {
${'top'.$c.'n'} = $name;
${'top'.$c.'p'} = $points;
$c++;
// Print out the contents of each row into a table
echo "<tr><td>$Name</td><td>$tokens</td></tr>";
}
echo "</table>";

Display result from database in two columns

EDIT: This is what I am trying to achieve: http://i.imgur.com/KE9xx.png
I am trying to display the results from my database in two columns. I'm a bit new to PHP so I haven't the slightest clue on how to do this. Can anybody help me with this? Thanks in advance.
Here is my current code:
include('connect.db.php');
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM todo ORDER BY id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table width='415' cellpadding='0' cellspacing='0'>";
// set table headers
echo "<tr><td><img src='media/title_projectname.png' alt='Project Name' /></td>
<td><img src='media/title_status.png' alt='Status'/></td>
</tr>";
echo "<tr>
<td><div class='tpush'></div></td>
<td> </td>
</tr>"
while ($row = $result->fetch_object())
{
echo "<tr>";
echo "<td><a href='records.php?id=" . $row->id . "'>" . $row->item . "</a></td>";
echo "<td>" . $row->priority . "</td>";
echo "</tr>";
}
echo "</table>";
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
A good idea would be storing your data into a simple array and then display them in a 2-columned table like this:
$con = mysql_connect('$myhost', '$myusername', '$mypassword') or die('Error: ' . mysql_error());
mysql_select_db("mydatabase", $con);
mysql_query("SET NAMES 'utf8'", $con);
$q = "Your MySQL query goes here...";
$query = mysql_query($q) or die("Error: " . mysql_error());
$rows = array();
$i=0;
// Put results in an array
while($r = mysql_fetch_assoc($query)) {
$rows[] = $r;
$i++;
}
//display results in a table of 2 columns
echo "<table>";
for ($j=0; $j<$i; $j=$j+2)
{
echo "<tr>";
echo "<td>".$row[$j]."</td><td>".$row[$j+1]."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
<table>
<tr>
<td>ProjectName</td>
<td>Status</td>
<td>ProjectName</td>
<td>Status</td>
</tr>
<?php
while($row = $result->fetch_object()) {
echo "<tr>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "</tr>";
}
?>
</table>
This is the thing on picture. With a bit CSS you can manipulate the tds.
Your function should look similar to this:
$query = "SELECT *
FROM todo
ORDER BY id";
$result = $mysqli->query($query);
while($row = $result -> fetch_array()) {
$feedback .= "<tr>\n<td>" . $row['item'] . "</td><td>" . $row['priority'] . "</td>\n</tr>";
}
return $feedback;
Then, in your HTML have the <table> already setup and where you would normally insert your <td> and <tr> put <?php echo $feedback?> (where $feedback is the assumed variable on the HTML page that retrieves the $feedback from the function). This isn't a complete fix, your code is hard to read, but by starting here, you should be able to continue on the path filling in all the extra information you need for the table, including your CSS.

Show an image next to a result from MySQL in PHP?

I'm pulling a table from a MySQL database, using PHP.
This is roughly the code I'm using, obviously I've got more columns, but this is the demo version of the code.
As you can see, the code is ordered by the Sales Name. Now what I want to do is, show a little icon next to the top ten sellers based on the Sales Count.
So, if a Sales Count is one of the top ten figures, it must show a little image next to it's name.
Can this be done?
<?php
// Get all the data from the "sales" table
$result = mysql_query("SELECT * FROM sales ORDER BY SalesName") or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Sales Name</th> <th>Sales Count</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo $row['SalesName'];
echo "</td><td>";
echo $row['SalesCount'];
echo "</td></tr>";
}
echo "</table>";
?>
Yes you can do it:
<?php
$item_no=1 // number of current items
// Get all the data from the "sales" table
$result = mysql_query("SELECT * FROM sales ORDER BY SalesCount desc SalesName ") or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Sales Name</th> <th>Sales Count</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
if($item_no <= 10){
echo 'your image here';
$item_no++;
}
echo $row['SalesName'];
echo "</td><td>";
echo $row['SalesCount'];
echo "</td></tr>";
}
echo "</table>";
?>
Try something like this :
$result = mysql_query("SELECT * FROM sales ORDER BY SalesName") or die(mysql_error());
$result2 = mysql_query("SELECT id FROM sales ORDER BY SalesCount DESC limit 10") or die(mysql_error());
while($savedResult = mysql_fetch_array($result2)) {
$topten[] = $savedResult[0];
}
echo "<table border='1'>";
echo "<tr> <th>Sales Name</th> <th>Sales Count</th> </tr>";
while ($row = mysql_fetch_array($result )) {
echo "<tr><td>";
echo $row['SalesName'];
if (in_array($row['id'],$topten)) {
echo '<img src="star.gif"/>';
}
echo "</td><td>";
echo $row['SalesCount'];
echo "</td></tr>";
}
echo "</table>";
This sends a second query which gets the id column for the top ten orders by sales count. I then store these ids in an array ($topten). While in the loop to display the table I check if the line being processed is in the top ten array - if so add a star !
you could do something like
<?php
// Get all the data from the "sales" table
$result = mysql_query("SELECT * FROM sales ORDER BY SalesName") or die(mysql_error());
$topTen = mysql_query("SELECT SalesName FROM sales ORDER BY SalesCount LIMIT 10") or die(mysql_error());
$topTenArray = array();
while($row = mysql_fetch_array( $result )) {
$topTenArray[] = $row['SalesName'];
}
echo "<table border='1'>";
echo "<tr> <th>Sales Name</th> <th>Sales Count</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo $row['SalesName'];
echo "</td><td>";
echo $row['SalesCount'];
if(in_array($row['SalesName'],$topTenArray){
echo "<img src.... />";
}
echo "</td></tr>";
}
echo "</table>";
?>

Categories