Creating dynamic table from db using php - php

I have tried but i could not just get where i am doing it wrong. The table only echo the first row continuously and did not echo the other rows. Kindly help please.
$run = "
SELECT * FROM staff
";
$runquery = mysqli_query($connection, $run);
$runrow = mysqli_num_rows($runquery);
if($runrow < 1){
echo "<p class='errormsg'>You do not have any Staff</p>";
}
else{
$row = mysqli_fetch_array($runquery);
if($row) {
$surname = $row['surname'];
$lastname = $row['lastname'];
$phone = $row['phone'];
$username = $row['username'];
$role = $row['auth'];
}
foreach ($row as $staff) {
$table .= "
<tr>
<td>$surname</td>
<td>$phone</td>
<td>$username</td>
<td>$role</td>
</tr>
";
}
}

You need to change loop like this. also the tabular view of your record require <table> body.
$run = "SELECT * FROM staff";
$runquery = mysqli_query($connection, $run);
$runrow = mysqli_num_rows($runquery);
if( $runrow < 1 ) {
echo "<p class='errormsg'>You do not have any Staff</p>";
}
else {
$table = ""; // create table here
while($row = mysqli_fetch_array($runquery)) {
$surname = $row['surname'];
$lastname = $row['lastname'];
$phone = $row['phone'];
$username = $row['username'];
$role = $row['auth'];
$table .= "<tr>
<td>$surname</td>
<td>$phone</td>
<td>$username</td>
<td>$role</td>
</tr>";
}
echo $table;
}

did you try to move the following code part into the foreach-loop?
$run = "
SELECT * FROM staff
";
$runquery = mysqli_query($connection, $run);
$runrow = mysqli_num_rows($runquery);
if($runrow < 1){
echo "<p class='errormsg'>You do not have any Staff</p>";
}
else{
$row = mysqli_fetch_array($runquery);
foreach ($row as $staff) {
$surname = $staff['surname'];
$lastname = $staff['lastname'];
$phone = $staff['phone'];
$username = $staff['username'];
$role = $staff['auth'];
$table .= "
<tr>
<td>$surname</td>
<td>$phone</td>
<td>$username</td>
<td>$role</td>
</tr>
";
}
}

Related

select all not working in mysql

pls i tried to display all content in a database, it keeps dispaying the last inserted, kindy help out.
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts ");
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags= $row["hashtags"];
?>
<?php
}
?>
<?php
echo $id;
echo $body;
?>
Either print the values in while loop or store them in an array to print them outside the loop.
while($row = mysql_fetch_assoc($query)){
echo $row["id"];
echo $row["body"];
}
condb.php
<?php
$con = mysqli_connect("localhost","username","password","databasename");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
index.php
<?php
include 'database/condb.php';
$query = "SELECT * FROM posts";
$res = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($res)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags= $row["hashtags"];
echo $id;
echo $body;
}
?>
For storing data, sometimes using array becomes helpful. I usually recommend it:
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts");
$array = [];
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags = $row["hashtags"];
$array[$id] = ['username'=>$username, 'body'=>$body, 'date_added'=>$date_added, 'hasttags'=>$hasttags];
}
?>
<?php
foreach($array as $id => $value){
echo $id; //Prints id
echo "<br>";
echo $value['username'].", ".$value['body'].", ".$value['date_added'].", ".$value['hasttags'];
}
?>
Or you can simply do this:
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts");
$array = [];
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags = $row["hashtags"];
?>
<?php
echo $id.", ".$username.", ".$body.", ".$date_added.", ".$hasttags;
}
?>
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts ");
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags= $row["hashtags"];
echo $id;
echo "<br>";
echo $body;
?>
<?php
}
?>
As an answer to the question "pls i tried to display all content in a database, it keeps dispaying the last inserted, kindy help out."
Put echo $id, echo $body be inside the loop.

How can I reset index to 0

i should make a "cheque" and there should be (Index,id,....), but index should reset after 100, but id must be auto_incremented.
Here is my code what i did. I didnt get how can i reset index to 0. I cant even show the indexs value, it shows 0 everytime when i add something.
<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "dbcar";
$number = "";
$state_number = "";
$model = "";
$status = "";
$conditions = "";
$date = "";
$number_index = "1";
$connect = mysqli_connect($host,$user,$password,$database);
echo "<h1>Report Log </h1>";
$sqlget = "SELECT * FROM carserv ORDER BY date DESC LIMIT 1";
$sqldata = mysqli_query($connect,$sqlget) or die("error");
echo "<table>";
echo"<tr>
<th>INDEX</th>
<th>ID</th>
<th>State Number</th>
<th>Model</th>
<th>Status</th>
<th>Condition</th>
<th>Date</th>
</tr>";
while($row = mysqli_fetch_array($sqldata,MYSQLI_ASSOC)){
echo" <tr><td>";
echo $row['number_index'];
echo" </td><td>";
echo $row['number'];
echo" </td><td>";
echo $row['state_number'];
echo" </td><td>";
echo $row['model'];
echo" </td><td>";
echo $row['status'];
echo" </td><td>";
echo $row['conditions'];
echo" </td><td>";
echo $row['date'];
echo" </td></tr>";
}
echo "</table>";
function getPosts(){
$posts = array();
$posts[0] = $_POST['state_number'];
$posts[1] = $_POST['number'];
$posts[2] = $_POST['model'];
$posts[3] = $_POST['status'];
$posts[4] = $_POST['conditions'];
$posts[6] = $_POST['number_index'];
return $posts;
}
?>
and here is my output: http://imgur.com/GOuCcBU
Take look in your phpmyadmin page there is auto_increment option you need to just have two field check auto increment for id field and for index you just fetch it and save it to db in another field with name number_index(because index is a reserved word).
Reset your db to O by doing something like this is your counter_update.php
if($_POST['index_reset']) {
$index_reset = $_POST[index_reset];
mysql_connect("server", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$sql = 'UPDATE counters SET number_index =\'0\' WHERE Page = \'$index_reset\';';
}
And html side something like this
$page_reset = "<form id='Reset' action='counter_update.php' method='post'>
<button type='submit' name='index_reset' value='$formPage'>RESET</button>
</form>";

MySQL: row is not updating when query execute by $_GET variable

I am having a problem again with my MySQL query. I am trying to execute my query using my $_GET variable. so here is my code. and many thanks in advance and answer will be very appreciated.
and please dont down vote my question if anyone thinks its not correct, please edit it or tell me to edit it.
many thanks.
here is Code.
$Status = $_GET['status'];
$User = $_GET['user'];
require('./connect.php');
$query = "SELECT * FROM users ORDER BY id ASC";
$result = mysqli_query($con, $query);
$numrows = mysqli_num_rows($result);
if ($numrows > 0) {
echo '<table class="table" border="1">
<tr style="background-color: #0DF; color: #222; font-weight:bold;">
<td>ID:</td>
<td>User Name:</td>
<td>Email:</td>
<td>First Name:</td>
<td>Last Name:</td>
<td>Domain:</td>
<td>Country:</td>
<td>Phone:</td>
<td>Plan:</td>
<td>Duration:</td>
<td>Payable:</td>
<td>Paid Date:</td>
<td>Active Plan:</td>
<td>Active:</td>
<td>Register Date:</td>
</tr>';
while ( $row = mysqli_fetch_assoc($result) ) {
$dbid = $row['id'];
$dbuser = $row['username'];
$dbemail = $row['email'];
$dbfname = $row['first_name'];
$dblname = $row['last_name'];
$dbdomain = $row['domain'];
$dbcountry = $row['country'];
$dbphone = $row['phone'];
$dbplan = $row['plan'];
$dbduration = $row['duration'];
$dbpayable = $row['payable'];
$dbpaid_date = $row['paid_date'];
$dbactive_plan = $row['active_plan'];
$dbactive = $row['active'];
$dbdate = $row['date'];
if ( $dbactive_plan == 0) {
$status = "Activate";
$changeStatus = ''.$status.'';
}
else {
$status = "Deactivate";
$changeStatus = ''.$status.'';
}
echo '
<tr>
<td>'.$dbid.'</td>
<td>'.$dbuser.' Delete</td>
<td>'.$dbemail.'</td>
<td>'.$dbfname.'</td>
<td>'.$dblname.'</td>
<td>'.$dbdomain.'</td>
<td>'.$dbcountry.'</td>
<td>'.$dbphone.'</td>
<td>'.$dbplan.'</td>
<td>'.$dbduration.'</td>
<td>'.$dbpayable.'</td>
<td>'.$dbpaid_date.'</td>
<td>'.$dbactive_plan.' '.$changeStatus.'</td>
<td>'.$dbactive.'</td>
<td>'.$dbdate.'</td>
</tr>';
}//while loop
echo '</table>';
if ($Status == 1 && $User == $dbuser) {
$query = "UPDATE users SET active_plan='$Status' WHERE username='$User'";
mysqli_query($con, $query);
echo "$Status";
}
else if ($Status == 0 && $User == $dbuser) {
$query = "UPDATE users SET active_plan='$Status' WHERE username='$User'";
mysqli_query($con, $query);
echo "$Status";
}
UPDATED
You're defining $dbuser inside the while loop, so it is getting set to the username from the last iteration of the loop. I'm not sure based on your code what $dbuser is supposed to be, judging by your code....

PHP MySQLi Query returning null with valid syntax

Ok so,
I have been trying to make a ticket system for my wxPanel in order to provide basic support for the application. Although I am easily able to make a database record with the provided code:
PHP:
if (isset($_POST['submit'])) {
$subject = $_POST['subject'];
$message = $_POST['message'];
$date = date('D M H:i');
$subject = mysqli_real_escape_string($subject);
$message = mysqli_real_escape_string($message);
$ticket_id = 'TICK_'.rand(00000,99999);
if (strlen($subject) === 0) {
echo "Subject Invalid.";
} elseif (strlen($message) === 0) {
echo "Message Invalid.";
} else {
mysqli_query("INSERT INTO tickets VALUES(
NULL,
'".$ticket_id."',
'".$_SESSION['user']."',
'".$subject."',
'1',
'".$date."',
'".$message."'
)");
}
header('Location: /view-ticket?identifier='.$ticket_id);
}
Works fine...
Then there is this, which is ment to fetch the ticket records and display the titles one by one:
PHP:
$query = mysqli_query("SELECT `subject`,`ticket_id` FROM tickets WHERE `username` = '".$_SESSION['user']."'");
while ($row = mysqli_fetch_assoc($query)) {
$tickets = $row['subject'];
$id = $row['ticket_id'];
}
foreach ($tickets as $ticket) {
echo '
<h2>'.$ticket.'</h2>
';
}
This always returns NULL. And also none of this works either:
if (isset($_GET['identifier']) === false || empty($_GET['identifier']) === true) {
header('Location: /tickets');
exit();
}
$id = mysqli_real_escape_string($_GET['identifier']);
$query = mysqli_query("SELECT `ticket_id`,`message`,`timestamp`,`status` FROM tickets WHERE `ticket_id` = '".$id."'");
while($row = mysqli_fetch_assoc($query)) {
$ticket_id = $row['ticket_id'];
$message = $row['message'];
$timestamp = $row['timestamp'];
$status = $row['status'];
}
foreach($message as $msg) {
echo '
<div class="ticket-message">
<h2>'.$message.'</h2>
</div>';
}
Thank you in advance!
p.s. Some of my code may be messy. Advice is always appreciated :)
Once you get the SELECT query working you are also going to have to look at the code that processes the results.
If ticket_id identifies a unique row
$query = mysqli_query($con, "SELECT ticket_id,message,timestamp,status
FROM tickets WHERE ticket_id = '$id'");
$row = mysqli_fetch_assoc($query);
$ticket_id = $row['ticket_id'];
$message = $row['message'];
$timestamp = $row['timestamp'];
$status = $row['status'];
echo '<div class="ticket-message"><h2>'.$message.'</h2></div>';
If ticket_id does not identify a unique row
$query = mysqli_query($con, "SELECT ticket_id,message,timestamp,status
FROM tickets WHERE ticket_id = '$id'");
// initialise the arrays that hold multiple row results
$ticket_id[] = array();
$message[] = array();
$timestamp[] = array();
$status[] = array();
while($row = mysqli_fetch_assoc($query)) {
$ticket_id[] = $row['ticket_id'];
$message[] = $row['message'];
$timestamp[] = $row['timestamp'];
$status[] = $row['status'];
}
foreach($message as $msg) {
echo '<div class="ticket-message"><h2>'.$msg.'</h2></div>';
}

MySQL query only fetching the first row instead of all the rows in the table

I'm trying to fetch multiple rows in MySQL database but it is fetching only the first entry!
$data = mysql_query("SELECT * FROM twitter_tbl")
while($info = mysql_fetch_array($data))
{
$desc = $info['description'];
$screen_name1 = $info['screen_name'];
$final_oauth_token = $info['final_oauth_token'];
$final_oauth_token_secret = $info['final_oauth_token_secret'];
);
So I get only 1 row but I need to get multiple rows as every row has got its unique screen_name and description.
I don't know where I'm going wrong.
Correct code to this:
$data = mysql_query("SELECT * FROM twitter_tbl");
while($info = mysql_fetch_array($data))
{
echo $desc = $info['description'];
echo $screen_name1 = $info['screen_name'];
echo $final_oauth_token = $info['final_oauth_token'];
echo $final_oauth_token_secret = $info['final_oauth_token_secret'];
}
While statements are enclosed within {} not {);
Edited:
$data = mysql_query("SELECT * FROM twitter_tbl")
while($info = mysql_fetch_array($data))
{
Echo $desc = $info['description'];
Echo $screen_name1 = $info['screen_name'];
Echo $final_oauth_token = $info['final_oauth_token'];
Echo $final_oauth_token_secret = $info['final_oauth_token_secret'];
}
You simply need to place it in a container that would display the results on your webpage
Try This:
echo "<table>";
$data = mysql_query("SELECT * FROM twitter_tbl");
while($info = mysql_fetch_array($data))
{
$desc = $info['description'];
$screen_name1 = $info['screen_name'];
$final_oauth_token = $info['final_oauth_token'];
$final_oauth_token_secret = $info['final_oauth_token_secret'];
echo "
<tr>
<td>".$desc."</td>
<td>".$screen_name1."</td>
<td>".$final_oauth_token."</td>
<td>".$final_oauth_token_secret."</td>
</tr>";
}
echo "</table>";
Adding data into array.
what is your target? (Store data? echo?)
CODE:
$data = mysql_query("SELECT * FROM twitter_tbl");
if($data && mysql_num_rows($data) > 0)
{
while($info = mysql_fetch_array($data))
{
$desc[$i] = $info['description'];
$screen_name1[$i] = $info['screen_name'];
$final_oauth_token[$i] = $info['final_oauth_token'];
$final_oauth_token_secret[$i] = $info['final_oauth_token_secret'];
++$i;
}
unset($i, $info, $data);
}
Or like Echo mode
CODE:
$data = mysql_query("SELECT * FROM twitter_tbl");
if($data && mysql_num_rows($data) > 0)
{
$x = "
<table>
<tr>
<th>
desc
</th>
<th>
screen_name
</th>
<th>
oauth Token
</th>
<th>
secret oauth Token
</th>
</tr>";
//Warning it's eat a lot of memory
while($info = mysql_fetch_array($data))
{
$x .= "
<tr>
<td>
{$info['description']}
</td>
<td>
{$info['screen_name']}
</td>
<td>
{$info['final_oauth_token']}
</td>
<td>
{$info['final_oauth_token_secret']}
</td>
</tr>";
}
$x = "
</table>";
echo $x;
unset($info, $data, $x);
}

Categories