Yo brothers, I am trying to echo all data from a table including column names and fields. I am able to successfully get the column names, I also want to get all the rows and fields, I am not sure if I am taking the right approach to solve my issue, please help.
//this is to echo coloumn names
$table = '<table border="1" cellspacing="10" cellpadding="10" style="width:60%">';
$table2 = "</table>";
$sql2 = "SHOW COLUMNS FROM $tablename";
$query2 = mysqli_query($conn,$sql2);
$tablename = $_POST['db'];
echo $table;
while($row = mysqli_fetch_array($query2)){
echo $row[0] . " ";
}
echo $table2 . "<br /><br />";
// successful in echoing coloumn names
// now i want to echo rows based on the number of coloumns
$countcol = mysqli_num_rows($query2);
$sql = "SELECT * FROM $tablename";
$query = mysqli_query($conn,$sql);
echo $table;
$i = "";
while($row=mysqli_fetch_array($query)){
for($i=0;$1<$countcol;$i++){
echo $row[0];
}
}
echo $table2 . "<br /><br />";
// this doenst get the all the rows based on the no of coloumns
in the final loop, is the echo $row[0] correct or should it be $row[$i]
Related
I am trying to display SQL table data with PHP, right now I have made an HTML table that infinitely loops up until the end of the page. Now I want to try and display 10~ names vertically before starting a new HTML table next to it that continues looping until the entire SQL table is displayed. The script I have right now is as follows.
$result = mysqli_query($conn, "SELECT name, id FROM table ORDER BY name");
while($row = mysqli_fetch_array($result)){
echo "<table>
<tr><td><input type=\"submit\" name=\"select\" value=\"" . $row['name'] . "\"></input></td></tr>
</table>" ;
}
if I understood correctly you want to have a table for each 10 rows , if thats the case you can do it as follows :
$result = mysqli_query($conn, "SELECT name, id FROM table ORDER BY name");
$i = 1;
while ($row = mysqli_fetch_array($result)) {
if ($i == 1) {
print "<table>";
} elseif ($i == 10) {
print " </table><table>";
$i = 1;
}
echo "
<tr><td><input type=\"submit\" name=\"select\" value=\"" . $row['name'] . "\"></input></td></tr>
";
$i++;
}
You can simply fetch all the rows into a table and then split it into chunks of 10.
$result = mysqli_query($conn, "SELECT name, id FROM table ORDER BY name");
$data = $result->fetch_all(MYSQLI_ASSOC);
foreach (array_chunk($data, 10) as $table) {
echo "<table>";
foreach ($table as $row) {
echo "<tr><td><input type=\"submit\" name=\"select\" value=\"" . $row['name'] . "\"></input></td></tr>";
}
echo "</table>";
}
I have an index.php with various SQL queries which helps me find the balances of the respective accounts. This is a TRIAL BALANCE so I need to SUM all the amount in the Debit Column and SUM all the amount in the Credit Column so that I can Tally both of them. Please help me with the SUM. All values are echoed to a PHP table.
index.php
<?php
echo "<table border='1'>";
echo "<tr><th width='150'>Account</th><th width='200'>Debit</th><th width='200'>Credit</th></tr>";
echo "<tr><th align='left'>Cash & Bank</th></tr>";
//List the Banks first with their respective balances.
$bank_sql = "SELECT * FROM bank";
$bank_query = mysqli_query($conn, $bank_sql);
while ($bank_result = mysqli_fetch_array($bank_query)){
$bank_name = $bank_result['name'];
$sql= "SELECT SUM(amount) FROM account WHERE (mode='$bank_name' AND status='completed') AND (type='p' OR type='r')";
$sql_query = mysqli_query($conn, $sql);
while($sql_result = mysqli_fetch_array($sql_query)){
$sql_value = $sql_result['SUM(amount)'];
}
$sql1 = "SELECT SUM(amount) FROM account WHERE (mode='$bank_name' AND status='completed') AND (type='s' OR type='pa')";
$sql1_query = mysqli_query($conn, $sql1);
while($sql1_result = mysqli_fetch_array($sql1_query)){
$sql1_value = $sql1_result['SUM(amount)'];
}
echo "<tr><td>".$bank_name."</td>";
if ($sql_value > $sql1_value){
echo "<td align='right'>AED " . number_format(($sql_value - $sql1_value),2) . "</td><td> </td>";
}
elseif ($sql1_value > $sql_value) {
echo "<td> </td><td align='right'>AED " . number_format(($sql1_value - $sql_value),2) . "</td>";
}
else {
echo "<td> </td><td> </td>";
}
}
echo "</tr></table>";
}
?>
Try the below query
SELECT SUM(amount) FROM ht_account WHERE mode='$bank_name' AND status='completed' AND type IN('sale','purchase','reciept','payment')
A quick way to do it would be.
$sql3="select column1,column2 from yourtable where ..condition";
$sumofcolumn1;
$sumofcolumn2;
while($sql3_result = mysqli_fetch_array($sql3))
{
$sumofcolumn2+=$sql3_result['column2'];
$sumofcolumn1+=$sql3_result['column1'];
}
echo $sumofcolumn1.' '.$sumofcolumn2
I have had a long road to get to this last question. Everything is my code is working now, but I can't get this last little issue. Right now I have:
$sql = "SELECT phonenumber,email, dataplan AS currentplan, SUM(datamb) AS
value_sum FROM maindata GROUP BY phonenumber, dataplan";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$val = $row["value_sum"];
$plan = $row["currentplan"];
$remain = $plan - $val;
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
It only subtracts the first value as opposed to the values for all. displayed like this:
while ($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['phonenumber'] . "</td> ";
echo "<td>".$row['currentplan'] . "</td> ";
echo "<td>".ROUND ($row["value_sum"],2) . "MB</td> ";
echo "<td>".$remain . " MB</td> ";
echo "<td>".$row['email'] . "</td></tr>";
}
So my goal is to subtract all value_sums from all dataplans, but what I have now, gives me the first value for all columns. Thank you!
mysql_fetch_assoc() will always get one row. You can use it in loop, or better use PDO, eg. like this:
$sql = "SELECT phonenumber,email, dataplan AS currentplan, SUM(datamb) AS
value_sum FROM maindata GROUP BY phonenumber, dataplan";
$results = $pdo->query($sql);
You can read about creating PDO connections here http://www.php.net/manual/en/book.pdo.php
Thanks for reading my question
i am trying to make *clients_id* from the table repair_jobs appear as the name from the table contacts
but i am having no luck
i have got 2 sql query's is this wrong?
the 1st
$query = "select * from repair_jobs";
this helps me display the information i need regarding the fields from repair_jobs and works
this is the 2nd
$query = "SELECT repair_jobs.client_id, contacts.name
FROM repair_jobs
INNER JOIN contacts
ON repair_jobs.client_id=contacts.name";
under that i have this to try to display the name of the client
echo "<td>{$client_id}</td>";
but it is only displaying the number and not the data (clients name) that i need
am i missing something?
Additional information
The client_id (repair_jobs) is a number and is the same as id (contacts) but wanting to display the name (contacts)
CLIENTS
Id – name – surname – phone – address
REPAIRS
Id – clients_id (same as id in clients) – unit – date – price
current code
<?php
//include database connection
include 'db_connect.php';
//query all records from the database
$query = "select * from repair_jobs";
//execute the query
$result = $mysqli->query( $query );
//get number of rows returned
$num_results = $result->num_rows;
//this will link us to our add.php to create new record
if( $num_results > 0){ //it means there's already a database record
//start table
//creating our table heading
echo " <table class='table_basic'>";
echo "<thead><tr>";
echo "<th>Job #</th>";
echo "<th>Name Of Unit</th>";
echo "<th>Client</th>";
echo "<th>Estimated Value</th>";
echo "</thead></tr><tbody><tr>";
//loop to show each records
while( $row = $result->fetch_assoc() ){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td width='40px'><a href='rdetails.php?id={$id}'># {$id}</a></td>";
echo "<td>{$rmake} {$rmodel}</td>";
$query = "SELECT rj.client_id, c.name AS client_name FROM repair_jobs rj INNER JOIN contacts c ON rj.client_id=c.id";
echo "<td>{$client_name}</td>";
echo '<td align="center"><span class="badge badge-success">£';
$lhours = $labour;
$repaircosts = $ourcosts;
$labourpay = $labourcharge;
$sum_total = $repaircosts +($lhours * $labourpay);
print ($sum_total);
echo '</span></td>';
echo "</td>";
echo "";
}
echo "</tr></table>";//end table
}else{
//if database table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
Change your 1st query to you join query, as there is no reason to do a 2nd query in the middle of your code. (also you never executed that query anyway).
//query all records from the database
$query = "SELECT repair_jobs.*, contacts.name as client_name
FROM repair_jobs
INNER JOIN contacts
ON repair_jobs.client_id=contacts.id";
Then in your table keep the $client_name
echo "<td>{$client_name}</td>";
<?php
include 'db_connect.php';
$query = "SELECT rj.Id AS job_number, rj.unit, rj.make, rj.model, c.name AS client_name, rj.price FROM repair_jobs rj INNER JOIN contacts c ON rj.clients_id = c.id ORDER BY c.date";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;
if( $num_results > 0){ //it means there's already a database record
echo " <table class='table_basic'>";
echo "<thead><tr>";
echo "<th>Job #</th>";
echo "<th>Name Of Unit</th>";
echo "<th>Client</th>";
echo "<th>Estimated Value</th>";
echo "</tr></thead><tbody>";
while( $row = $result->fetch_assoc() ){
extract($row);
echo "<tr>";
echo "<td width='40px'><a href='rdetails.php?id={$job_number}'>#{$job_number}</a></td>";
echo "<td>{$make} {$model}</td>";
echo "<td>{$client_name}</td>";
echo "<td align='center'><span class='badge badge-success'>£";
$lhours = $labour;
$repaircosts = $ourcosts;
$labourpay = $labourcharge;
$sum_total = $repaircosts +($lhours * $labourpay);
echo $sum_total;
echo '</span></td>';
echo "</td>";
echo "</tr>";
}
echo "</tbody></table>";
} else {
echo "No records found.";
}
$result->free();
$mysqli->close();
?>
How can I make those dates line up with start date and end date or selectively echo the dates rather than have the while loop echo them?
For example if I can echo "Start Date" then the actual date then the "end date" and then the date.
Basically how may I echo the dates from mySQL selectively rather than using the loop to echo them?
for example if I want to echo row 2 can I do echo $row'date' (to echo the 2nd row)
On HTML Page PHP CODE:
Start Date:
End Date:
<?php
mysql_connect("host","username","password") or die(mysql_error());
mysql_select_db("UserLogins") or die(mysql_error());
$query = "SELECT * FROM Date1";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<li>".$row['date']."</li>";
echo "<br />";
}
?>
It sounds like to you need to constrain your query results. If you only want certain records from your database then you should filter out the ones you don't want right in the SQL.
$query = "SELECT * FROM Date1 WHERE date >= ? AND date <= ?";
If you use prepared statements you can plug in values for the two questions marks.
first solution
$arr = Array();
while($row = mysql_fetch_array($result)){
$arr[] = $row;
}
echo $arr[0]['date']; // first
echo $arr[3]['date'];
echo $arr[7]['date'];
echo $arr[count($arr)-1]['date']; // last
second solution
$first = #mysql_fetch_assoc(mysql_query("SELECT * FROM Date1 ORDER BY date ASC LIMIT1"));
$last = #mysql_fetch_assoc(mysql_query("SELECT * FROM Date1 ORDER BY date DESC LIMIT1"));
echo $first['date'].'<br>';
echo $last['date'];
if you want to use second solution remember to put index on date column
also...
<?php
mysql_connect("host","username","password") or die(mysql_error());
mysql_select_db("UserLogins") or die(mysql_error());
$query = "SELECT min(date) as startdate, max(date) as enddate FROM Date1";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
if($row) {
echo "Start Date: " . $row['startdate'] . "<br />\n";
echo "End Date: " . $row['enddate'] . "<br />\n";
}
?>
???
err...
<?php
mysql_connect("host","username","password") or die(mysql_error());
mysql_select_db("UserLogins") or die(mysql_error());
$query = "SELECT * FROM Date1";
$result = mysql_query($query) or die(mysql_error());
echo "<table border=\"0\">\n<tr><th>Start Date</th><th>End Date</th></tr>";
$startRow = true;
while($row = mysql_fetch_array($result)) {
if($startRow) {
echo "\n<tr><td>";
} else {
echo "<td>";
}
echo $row['date'];
if($startRow) {
echo "</td>";
} else {
echo "</td></tr>";
}
$startRow = !$startRow;
}
if(!$startRow) {
echo "</tr>";
}
echo "\n</table>\n";
?>
???