Why table fields are not showing values like first three fields? - php

Here is some short Code of table.I am using echo in table.
The data is not printing as first three fields.
echo "<table>";
echo "<tr>
<th>ID</th>
<th>Date</th>
<th>Reference</th>
</tr>" ;
while ($row = $result->fetch_object())
{
echo "<td>" . $row->id . "</td>";
echo "<td>" . $row->date . "</td>";
echo "<td>" . $row->ref . "</td>";
}
echo " <tr>
<th>First Name</th>
<th>Father Name</th>
<th>Phone</th>
</tr>";
while ($row = $result->fetch_object())
{
echo "<td>" . $row->name . "</td>";
echo "<td>" . $row->fname . "</td>";
echo "<td>" . $row->cell . "</td>";
}
echo "<tr>
<th>District</th>
<th>Address</th>
<th>Gender</th>
</tr>";
while ($row = $result->fetch_object())
{
echo "<td>" . $row->district . "</td>";
echo "<td>" . $row->address . "</td>";
echo "<td>" . $row->gender . "</td>";
}
echo "</table>";
Table Output Image

You have missed tr in every dynamic data. change your code as below:
echo "<table>";
echo "<tr>
<th>ID</th>
<th>Date</th>
<th>Reference</th>
</tr>" ;
while ($row = $result->fetch_object())
{
echo "<tr><td>" . $row->id . "</td>";
echo "<td>" . $row->date . "</td>";
echo "<td>" . $row->ref . "</td></tr>";
}
echo " <tr>
<th>First Name</th>
<th>Father Name</th>
<th>Phone</th>
</tr>";
while ($row = $result->fetch_object())
{
echo "<tr><td>" . $row->name . "</td>";
echo "<td>" . $row->fname . "</td>";
echo "<td>" . $row->cell . "</td></tr>";
}
echo "<tr>
<th>District</th>
<th>Address</th>
<th>Gender</th>
</tr>";
while ($row = $result->fetch_object())
{
echo "<tr><td>" . $row->district . "</td>";
echo "<td>" . $row->address . "</td>";
echo "<td>" . $row->gender . "</td></tr>";
}
echo "</table>";

Related

How to concatenate a row variable taken from a different query to an existing query?

I was hoping to get anyone's opinion on why this certain variable won't print out. Variable $sum from the $qsl_ query is not printing along with the other table row data.
<?php
$sql_ = "SELECT SUM(`points`) AS value_sum FROM `history` WHERE `userid` = '$id'";
$result = mysql_query($sql_);
#echo mysql_error();
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
$sql = "SELECT * FROM users";
$myData = mysql_query($sql);
echo "<table id=\"table\" class=\"table table-striped\">
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>Model</th>
<th>Year</th>
<th>Plate Number</th>
<th>City</th>
<th>Country</th>
<th>Points</th>
</tr></thead>";
while($record = mysql_fetch_array($myData)){
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['email'] . "</td>";
echo "<td>" . $record['firstName'] . "</td>";
echo "<td>" . $record['lastName'] . "</td>";
echo "<td>" . $record['model'] . "</td>";
echo "<td>" . $record['Year'] . "</td>";
echo "<td>" . $record['plateNumber'] . "</td>";
echo "<td>" . $record['city'] . "</td>";
echo "<td>" . $record['country'] . "</td>";
echo "<td>" . $sum . "</td>";
}
echo "</table>";
?>
This is my current output where the points column won't produce:
I apologize if my code appears inefficient, I am a struggling student fanatic of PHP. I appreciate direct answers; however, learning is my key here. Thanks in advance!
$sql = "SELECT users.*, sum(history.points) as points FROM users left join history on users.id=history.userid group by users.id";
$myData = mysql_query($sql);
echo "<table id=\"table\" class=\"table table-striped\">
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>Model</th>
<th>Year</th>
<th>Plate Number</th>
<th>City</th>
<th>Country</th>
<th>Points</th>
</tr></thead>";
while($record = mysql_fetch_array($myData)){
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['email'] . "</td>";
echo "<td>" . $record['firstName'] . "</td>";
echo "<td>" . $record['lastName'] . "</td>";
echo "<td>" . $record['model'] . "</td>";
echo "<td>" . $record['Year'] . "</td>";
echo "<td>" . $record['plateNumber'] . "</td>";
echo "<td>" . $record['city'] . "</td>";
echo "<td>" . $record['country'] . "</td>";
echo "<td>" . $record['points'] . "</td>";
}
echo "</table>";
It seems that I might have had my code jungled, thank you #abhi and #msfoster for your healthy inputs.
<?php
$sql = "SELECT * FROM users";
$myData = mysql_query($sql);
echo "<table id=\"table\" class=\"table table-striped\">
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>Model</th>
<th>Year</th>
<th>Plate Number</th>
<th>City</th>
<th>Country</th>
<th>Points</th>
</tr></thead>";
while($record = mysql_fetch_array($myData)){
$id = $record['id'];
$sql_ = "SELECT SUM(`points`) AS value_sum FROM `history` WHERE `userid` = '$id'";
$result = mysql_query($sql_);
#echo mysql_error();
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
echo "<tr>";
echo "<td>" . $id . "</td>";
echo "<td>" . $record['email'] . "</td>";
echo "<td>" . $record['firstName'] . "</td>";
echo "<td>" . $record['lastName'] . "</td>";
echo "<td>" . $record['model'] . "</td>";
echo "<td>" . $record['Year'] . "</td>";
echo "<td>" . $record['plateNumber'] . "</td>";
echo "<td>" . $record['city'] . "</td>";
echo "<td>" . $record['country'] . "</td>";
echo "<td>" . $sum . "</td>";
echo "</tr>";
}
echo "</table>";
?>
My output is finally:
query is correct but as you shown
var_dump($row) = "array (size=1) 'value_sum' => null "
so it have 'null' because of that its showing blank;
but for this you can use join also .
$row = mysql_fetch_assoc($result); returns an array of "rows". Since you want the first row you need to access it $sum = $row[0]['value_sum'];

How can I retrieve specific information from my sql query based on what information is filled in the HTML form fields?

I am brand new to PHP and everything else, so please try to keep any response as simple as possible. Thank you. I just can't figure out how to only get the information from the field that is filled in. When I do my search, it brings up the empty fields as well. I need just the information in the HTML form to be found. For example, if I put in a project code, I need all information that pertains to that code. Or if I put in a specific budget, I need everything that has that budget. I hope this makes sense...I'm such a rookie, I don't even know how to word the question!!!! I have tried using !is_null, (isset), !empty but none of them seem to work.
<?php
include 'connect.php';
// Get values from form
$p_code=$_GET['projectcode'];
$p_name=$_GET['projectname'];
$budget=$_GET['budget'];
$s_date=$_GET['startdate'];
$e_date=$_GET['enddate'];
$c_year=$_GET['cyear'];
$c_qtr=$_GET['cqtr'];
$c_grp=$_GET['cgrp'];
$sponsor=$_GET['sponsor'];
$client=$_GET['client'];
$p_lead=$_GET['projectlead'];
$orig_hrs=$_GET['originalhrs'];
$risk_per=$_GET['riskpercent'];
$notes=$_GET['notes'];
if ($db_found) {
$SQL = "SELECT * FROM minimodtable
WHERE projectcode='$p_code'
OR projectname='$p_name'
OR budget='$budget'
OR startdate='$s_date'
OR enddate='$e_date'
OR cyear='$c_year'
OR cqtr='$c_qtr'
OR cgrp='$c_grp'
OR sponsor='$sponsor'
OR client='$client'
OR projectlead='$p_lead'
OR originalhrs='$orig_hrs'
OR riskpercent='$risk_per'
OR notes='$notes'";
$result = mysqli_query($connect, $SQL);
echo "<table border='1'>
<tr>
<th>Project Code</th>
<th>Project Name</th>
<th>Budget</th>
<th>Start Date</th>
<th>End Date</th>
<th>Year</th>
<th>Quarter</th>
<th>Group</th>
<th>Sponsor</th>
<th>Client</th>
<th>Project Lead</th>
<th>Original Hours</th>
<th>Risk Percent</th>
<th>Notes</th>
</tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
echo "<tr>";
echo "<td>" . $row['projectcode'] . "</td>";
echo "<td>" . $row['projectname'] . "</td>";
echo "<td>" . $row['budget'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['enddate'] . "</td>";
echo "<td>" . $row['cyear'] . "</td>";
echo "<td>" . $row['cqtr'] . "</td>";
echo "<td>" . $row['cgrp'] . "</td>";
echo "<td>" . $row['sponsor'] . "</td>";
echo "<td>" . $row['client'] . "</td>";
echo "<td>" . $row['projectlead'] . "</td>";
echo "<td>" . $row['originalhrs'] . "</td>";
echo "<td>" . $row['riskpercent'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
}
echo "</table>";
}
mysqli_close($connect);
?>
You've made a good first try there. This method should save you having to repeat yourself so much, though. You were correct to start looking at isset - the issue is that you need to dynamically build up your SQL query.
In fact, you'll probably want to change the glue in the implode() function from 'OR' to 'AND' - that way, you can find things that are a specific budget AND that pertain to a certain code.
I've switched it to AND for now - but feel free to switch it back to OR - as per your example.
<?php
// All possible parameters
$params = array(
'projectcode',
'projectname',
'budget',
'startdate',
'enddate',
'cyear',
'cqtr',
'cgrp',
'sponsor',
'client',
'projectlead',
'originalhrs',
'riskpercent',
'notes'
);
$wheres = array();
foreach ($params as $param) {
// Is the param set?
// If so, let's add it to our list of WHERE clauses
if (isset($_GET[$param])) {
$wheres[] = sprintf("%s = '%s'", $param, mysqli_real_escape_string($connect, $_GET[$param]));
}
}
if ($db_found) {
// Now let's make the SQL.
$SQL = 'SELECT * FROM minimodtable';
// We only want to add the WHERE clause if we had some parameters passed
if (count($wheres) > 0) {
$SQL .= ' WHERE ' . implode(' AND ', $wheres);
}
$result = mysqli_query($connect, $SQL);
echo "<table border='1'>
<tr>
<th>Project Code</th>
<th>Project Name</th>
<th>Budget</th>
<th>Start Date</th>
<th>End Date</th>
<th>Year</th>
<th>Quarter</th>
<th>Group</th>
<th>Sponsor</th>
<th>Client</th>
<th>Project Lead</th>
<th>Original Hours</th>
<th>Risk Percent</th>
<th>Notes</th>
</tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
echo "<tr>";
echo "<td>" . $row['projectcode'] . "</td>";
echo "<td>" . $row['projectname'] . "</td>";
echo "<td>" . $row['budget'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['enddate'] . "</td>";
echo "<td>" . $row['cyear'] . "</td>";
echo "<td>" . $row['cqtr'] . "</td>";
echo "<td>" . $row['cgrp'] . "</td>";
echo "<td>" . $row['sponsor'] . "</td>";
echo "<td>" . $row['client'] . "</td>";
echo "<td>" . $row['projectlead'] . "</td>";
echo "<td>" . $row['originalhrs'] . "</td>";
echo "<td>" . $row['riskpercent'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
}
echo "</table>";
}
mysqli_close($connect);

Output not displaying all rows

Having an issue with the output of records from a query run to display records.... It only shows the first row as the code specifies and then the next results all in.. paragraphs? I don't know if it has something to do
<?php
include 'core/init.php';
include 'includes/overall/header.php';
?>
<div class="article" style="width:900px !important">
<?php
$result = $sql = mysql_query("SELECT * FROM ref_employees WHERE employerid={$user_data['user_id']} ")
or die('Error in query : $sql. ' .mysql_error());
echo "<table border='0' class='table'>
<tr>
<th>ID Number</th>
<th>Employee Number</th>
<th>FirstName</th>
<th>LastName</th>
<th>MiddleName</th>
<th>Job Title</th>
<th>Employement Status</th>
<th>Contact</th>
<th>Email</th>
<th>Edit</th>
</tr>";
if (mysql_num_rows($sql) > 0)
{
while ($row = mysql_fetch_array($sql)){
if ($row['employed'] == '1'){
echo "<tr>";
echo "<td>" . $row['idnumber'] . "</td>";
echo "<td>" . $row['empnumber'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['middlename'] . "</td>";
echo "<td>" . $row['jobtitle'] . "</td>";
echo "<td>" . $row['employed'] . "</td>";
echo "<td>" . $row['contactnum'] . "</td>";
echo "<td>" . $row['contactemail'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "</tr>";
echo "</tr>";
echo "</table>";
}
}
}
?>
</div>
<?php include 'includes/overall/footer.php';
?>
You are using closing table tag into loop as
while ($row = mysql_fetch_array($sql)){
....
....
...
echo "</table>";
}
use table closing tag out of loop as
while ($row = mysql_fetch_array($sql)){
....
....
...
}
echo "</table>";

PHP Button on each row of table

im making some application form in PHP.
im putting all info returned from the database into a table.
Now i want to create a button on each line that changes something in the DB of that line.
but i have no idea to do that :S
Thank you!
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>age</th>
<th>position</th>
<th>experience</th>
<th>motivation</th>
<th>date</th>
<th>status</th>
<th>test</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['position'] . "</td>";
echo "<td>" . $row['exp'] . "</td>";
echo "<td>" . $row['motivation'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . '<input type="submit" name="submit" value="accept">' . "</td>";
echo "</tr>";
}
echo "</table>";
EDIT: get it working using another script:
echo "<td>Approve</td>";
and edit.php:
<?php
include("dbconnect.php");
$member_id = $_GET['id'];
$status = $_GET['status'];
echo $member_id;
echo $status;
if ($status == 'app')
$query = "update apps set status = 'approved' where id = $member_id";
mysql_query($query) or die (mysql_error());
?>
Create small form with parameters in the cell you want the button to do smth. This is the simpliest approach (approach with refresh).
One more solution is to use AJAX on button click and forward action to some endpoint. This way it would be dynamic and probably what you are want to implement.
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>age</th>
<th>position</th>
<th>experience</th>
<th>motivation</th>
<th>date</th>
<th>status</th>
<th>test</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['position'] . "</td>";
echo "<td>" . $row['exp'] . "</td>";
echo "<td>" . $row['motivation'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . '<form type="POST"><input type="hidden" name="whatever" value="$row['id']"><input type="submit" name="submit_btn" value="accept"></form>' . "</td>";
echo "</tr>";
}
echo "</table>";
in this way, you could get a form in each row of the table. Now, you just need to use php POST function.
if(isset($_POST['submit_btn']))
{
//whatever u need to do
}

display multiple table as query result using ORDER BY in PHP MYSQL

I need a help on these, where I want to display the query result with multiple table as the assets have different attributes, it will be ORDER BY category. Let's say, Category = Laptop will list all the laptop details, TV will have its own table with its features & so on. All of this will be on the same page but breakdown by tables. How can I achieve this? Here's the part where I suppose the problem lies. Any help is highly appreciated!
$result = mysql_query($sql) or die (mysql_error());
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
$assetid = $row['assetid'];
$name = $row['name'];
$category = $row['category'];
$manufacturer = $row['manufacturer'];
$type = $row['type'];
$size = $row['size'];
$price = $row['price'];
$warranty = $row['warranty'];
$description = $row['description'];
if ($category == "1 - LAPTOP")
{
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Price</th>
<th>Warranty</th>
<th>Description</th>
</tr>";
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $price . "</td>";
echo "<td>" . $warranty . "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
}
elseif ($category == "2 - TV")
{
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Price</th>
<th>Warranty</th>
<th>Description</th>
</tr>";
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $price . "</td>";
echo "<td>" . $warranty. "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
}
elseif ($subassetcategory == "3 - DESK")
{
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Price</th>
<th>Description</th>
</tr>";
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $price . "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
}
elseif ($subassetcategory == "4 - TELEPHONE")
{
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Description</th>
</tr>";
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
}
}
}
else
{
echo "<br> No record found </br>";
}
The big problem I see in your code is its repetition, it completely breaks the DRY principle. Also, you have your categories hard coded in your code, and stored in your DB. I modified the script, so that it should create now a generic table header whenever a new category is found in the resultset.
Please try this (instead of all your code) and see if it works for you:
$categ = '';
$result = mysql_query($sql) or die (mysql_error());
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
$assetid = $row['assetid'];
$name = $row['name'];
$category = $row['category'];
$manufacturer = $row['manufacturer'];
$type = $row['type'];
$size = $row['size'];
$price = $row['price'];
$warranty = $row['warranty'];
$description = $row['description'];
if ($category != $categ)
{
$categ = $category;
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Price</th>
<th>Warranty</th>
<th>Description</th>
</tr>";
}
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $price . "</td>";
echo "<td>" . $warranty . "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
} //while
} //if
This code assumes that your results are comming with ORDER BY category
What about doing it like this? :
$result = mysql_query($sql) or die (mysql_error());
if(mysql_num_rows($result) > 0)
{
if ($category == "1 - LAPTOP")
{
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Price</th>
<th>Warranty</th>
<th>Description</th>
</tr>";
}
while($row = mysql_fetch_array($result))
{
$assetid = $row['assetid'];
$name = $row['name'];
$category = $row['category'];
$manufacturer = $row['manufacturer'];
$type = $row['type'];
$size = $row['size'];
$price = $row['price'];
$warranty = $row['warranty'];
$description = $row['description'];
if ($category == "1 - LAPTOP")
{
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $price . "</td>";
echo "<td>" . $warranty . "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
}
}
}
else
{
echo "<br> No record found </br>";
}

Categories