Hello here is my code :
<table class="table table-hover table-bordered">
<tr>
<th class="well" style="text-align:center">ID</th>
<th class="well" style="text-align:center">Email</th>
<th class="well" style="text-align:center">User level</th>
</tr>';
<?
$code_sql = "SELECT user_id, user_email,user_level FROM users ORDER BY user_id ASC ";
$code_query = mysql_query($code_sql) or die(error_sql(mysql_error(),__LINE__,__FILE__));
$sql_rows = mysql_num_rows($code_query);
if($sql_rows > 0){
while($rows = mysql_fetch_object($code_query)){
$user_id = intval($rows->user_id);
$user_level= intval($rows->user_level);
$user_email = htmlspecialchars($rows->user_email);
echo ' <tr>
<td>'.$user_id.'</td>
<td>'.$user_email.'</td>
<td>'.$user_level.'</td>
</tr>';
}
mysql_free_result($code_query);
}else{
echo '<tr>
<td>
<font color="red">no data found</font>
</td>
</tr>';
}
echo '</table>';
?>
the output of the code will be like .
<table class="table table-hover table-bordered">
<tr>
<th class="well" style="text-align:center">ID</th>
<th class="well" style="text-align:center">Email</th>
<th class="well" style="text-align:center">User level</th>
</tr>
<tr>
<td>1</td>
<td>test1#gmail.com</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>test2#gmail.com</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>test3#gmail.com</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>test4#gmail.com</td>
<td>1</td>
</tr>
</table>
(1 = normal user , and 2 = admin for user level) but what I want is something .like that
<table class="table table-hover table-bordered">
<tr>
<th class="well" style="text-align:center">ID</th>
<th class="well" style="text-align:center">Email</th>
<th class="well" style="text-align:center">User level</th>
</tr>
<div id="users">
<tr>
<td>1</td>
<td>test1#gmail.com</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>test4#gmail.com</td>
<td>1</td>
</tr>
</div>
<div id="admins">
<tr>
<td>2</td>
<td>test2#gmail.com</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>test3#gmail.com</td>
<td>2</td>
</tr>
</div>
</table>
I want to add a div id="users" that will contain all users from database that have user_level = 1 and another div id="admins" for user_level = 2.
try the following code, you need to create div's based on conditions like if the iteration number 1 then insert div(open) before content and after content(close)...and the if iteration number is 3 then again insert the div(open) before content an after content(close)
<table class="table table-hover table-bordered">
<tr>
<th class="well" style="text-align:center">ID</th>
<th class="well" style="text-align:center">Email</th>
<th class="well" style="text-align:center">User level</th>
</tr>
<?php
$code_sql = "SELECT user_id, user_email,user_level FROM users ORDER BY user_id ASC ";
$code_query = mysql_query($code_sql) or die(error_sql(mysql_error(),__LINE__,__FILE__));
$sql_rows = mysql_num_rows($code_query);
if($sql_rows > 0){
$i=0;
while($rows = mysql_fetch_object($code_query)){
$user_id = intval($rows->user_id);
$user_level= intval($rows->user_level);
$user_email = htmlspecialchars($rows->user_email);
if($i==0){?><div id='users'><?php }elseif($i==2){?><div id='admins'><?php }
echo ' <tr>
<td>'.$user_id.'</td>
<td>'.$user_email.'</td>
<td>'.$user_level.'</td>
</tr>';
if($i==0){?></div><?php }elseif($i==2){?></div><?php }
$i++;
}
mysql_free_result($code_query);
}else{
echo '<tr>
<td>
<font color="red">no data found</font>
</td>
</tr>';
}
echo '</table>';
?>
Try the below code with tbody to group table rows,
<?php
$code_sql = "SELECT user_id, user_email,user_level FROM users ORDER BY user_level ASC, user_id ASC ";
$code_query = mysql_query($code_sql) or die(error_sql(mysql_error(),__LINE__,__FILE__));
$sql_rows = mysql_num_rows($code_query);
if($sql_rows > 0){
$newgroup = false;
$groupid = array(1=>'users', 2=>'admins');
while($rows = mysql_fetch_object($code_query)){
$user_id = intval($rows->user_id);
$user_level= intval($rows->user_level);
$user_email = htmlspecialchars($rows->user_email);
if($newgroup != $user_level) {
if($newgroup != false) echo '</tbody>';
echo '<tbody id="'.$groupid[$user_level].'">';
$newgroup = $user_level;
}
echo ' <tr>
<td>'.$user_id.'</td>
<td>'.$user_email.'</td>
<td>'.$user_level.'</td>
</tr>';
}
echo '</tbody>';
mysql_free_result($code_query);
}else{
echo '<tr>
<td>
<font color="red">no data found</font>
</td>
</tr>';
}
echo '</table>';
?>
Note:
It would work if you just want to group table rows. But if you want to add additional features like animation then you might need to add more css/jQuery.
Additional suggestions:
1. You should start using mysqli or PDO
2. You should start using css to replace font tags.
Related
So in this query below im joining two tables through order_id and displaying the all the values from the user_orders table.
As per the image below I am trying to display only the order_Id rows that match the order_manager table.
public function getUserOrder(){
$sql = "SELECT user_orders.order_id,
user_orders.title, user_orders.price,
user_orders.quantity
FROM order_manager
JOIN user_orders ON order_manager.order_id = user_orders.order_id;";
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
while ($result = $stmt->fetchAll()){
return $result;
}
}
I have attempted to use an if statement that appears to do something however it gives me the values that don't match order id in reverse.
<div class="container mt-5">
<?php $artworks = new Artworks(); ?>
<div class="row">
<div class="col-lg-12">
<table class="table table-dark">
<thead>
<tr>
<th scope="col">Order ID</th>
<th scope="col">Full Name</th>
<th scope="col">Phone</th>
<th scope="col">Address</th>
<th scope="col">Orders</th>
</tr>
</thead>
<?php
$artworks->getOrder();
foreach ($artworks->getOrder() as $art) {
echo "<tbody>
<tr>
<td>$art[order_id]</td>
<td> $art[full_name]</td>
<td> $art[phone] </td>
<td>$art[address]</td>
<td>
<table class= 'tale text-center table-dark'>
<thead>
<tr>
<th scope='col'>Order ID</th>
<th scope='col'>title</th>
<th scope='col'>price</th>
<th scope='col'>Quantity</th>
</tr>
<thead>
<tbody>
<tr>";
$artworks->getUserOrder();
foreach ($artworks->getUserOrder() as $order) {
if ($order['order_id'] == $art['order_id']) {
echo "<td>$order[order_id]</td>";
}
echo "
<td>$order[title]</td>
<td>$order[price]</td>
<td>$order[quantity]</td>
</tr>";
}
echo "
</tbody>
</table>
</td>
</tr>
";
}
?>
</tbody>
</table>
</div>
</div>
</div>
Here is an image to help explain the desired output
Solved moving the td items into the if statement. Guess it was mostly correct.
$artworks->getUserOrder();
foreach($artworks->getUserOrder() as $order)
{
if ($order['order_id'] == $art['order_id']) {
echo "<td>$order[order_id]</td>";
echo "
<td>$order[title]</td>
<td>$order[price]</td>
<td>$order[quantity]</td>
</tr>
";
}
}
Right now I am writing a program in core PHP, but I am not able to write a perfect oops concept as I m rewriting the code many times.
for Eg.
public function all()
{
$init = new Database;
$sql = "SELECT * FROM categories";
// Result
$result = $init->conn->query($sql);
if ($result->num_rows > 0) {
?>
<table class="table table-hover">
<thead>
<tr>
<th>Action</th>
<th scope="col">id</th>
<th scope="col">Category name</th>
<th scope="col">Category description</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td>
<form action="update_categories.php?update_id=<?php echo $row['id'] ?>" method="post">
<button type="submit" class="btn btn-primary" name="update">Update</button>
</form>
<form action="delete_categories.php?id=<?php echo $row['id'] ?>" method="post">
<button type="submit" name="delete" class="btn btn-danger">Delete</button>
</form>
</td>
<td><?php echo $row["id"] ?></td>
<td><?php echo $row["category_name"] ?></td>
<td><?php echo $row["category_description"] ?></td>
</tr>
<?php
} ?>
</tbody>
<?php
} else {
?>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">Category name</th>
<th scope="col">Category description</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">no result</th>
<td>no result</td>
<td>no result</td>
</tr>
</tbody>
<?php
}
}
So here you can see I have to fetch the data from the database but in this function, I have included Html too so if you want to add database data to another place I have to write the function for that again and fetch the values.
Is there any way I can only fetch the value and i can use the function to print it wherever I want.
first question here, my problem is this.
I've got 22 more or less values to print stored inside a table in a DB. Problem is some of this values gotta have HTML tags in it, mainly tags <s> .
<?php
$query = "SELECT * FROM teams";
$result = mysqli_query($db, $query);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "
<div class='col-md-12'>
<div class='card strpied-tabled-with-hover'>
<div class='card-header '>
<h4 class='card-title'>".$row['name']." ".$row['surname']."</h4>
<p class='card-category'>".$row['team']."</p>
</div>
<div class='card-body table-full-width table-responsive'>
<table class='table table-hover table-striped'>
<thead>
<th>#</th>
<th>Player</th>
<th>Player Bonus</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td style='color:red; font-weight:bold'>".$row['c1']."</td>
<td>".$row['c1b']."</td>
</tr>
<tr>
<td>2</td>
<td>".$row['c2']."</td>
<td>".$row['c2b']."</td>
</tr>
<tr>
<td>3</td>
<td>".$row['c3']."</td>
<td>".$row['c3b']."</td>
</tr>
<tr>
<td>4</td>
<td>".$row['c4']."</td>
<td>".$row['c4b']."</td>
</tr>
<tr>
<td>5</td>
<td>".$row['c5']."</td>
<td>".$row['c5b']."</td>
</tr>
<tr>
<td>6</td>
<td>".$row['c6']."</td>
<td>".$row['c6b']."</td>
</tr>
<tr>
<td>7</td>
<td>".$row['c7']."</td>
<td>".$row['c7b']."</td>
</tr>
<tr>
<td>8</td>
<td>".$row['c8']."</td>
<td>".$row['c8b']."</td>
</tr>
<tr>
<td>9</td>
<td>".$row['c9']."</td>
<td>".$row['c9b']."</td>
</tr>
<tr>
<td>10</td>
<td>".$row['c10']."</td>
<td>".$row['c10b']."</td>
</tr>
<tr>
<td>11</td>
<td>".$row['c11']."</td>
<td>".$row['c11b']."</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>";} ?>
TL;DR: $row[c1] and so on have HTML tags stored as values like <s>stringvalue</s> but it just echo stringvalue. What can I do? Thanks and sorry for my inexperience.
EDIT: I mispelled the title in an horrific way.
i have problem with my table inside my table. it removes the last data in the second table.
the first table gives the correct output but the second table gives the wrong output. the second table can be shown by clicking the table row. the table row is expandable.
this is my code
<div class="form-group">
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
$stmt1 = $conn->prepare("select * from table where name=?");
$stmt1->bindParam(1, $_temppp1, PDO::PARAM_STR, 30);
$stmt1->execute();
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
<table id="report" class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)): ?>
<tr>
<td><?php echo $row1['tracknum']; ?></td>
<td><?php echo $row1['doctitle']; ?></td>
<td><?php echo $row1['doctype']; ?></td>
<td><?php echo $row1['datefilled']; ?></td>
<td>
<div class="arrow"><a id="menu-toggle" href="#" ><i class="glyphicon glyphicon-plus-sign"></i></a></div>
</td>
</tr>
<tr><td colspan="5">
<table class="table">
<thead>
<tr>
<th>Row</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John</td>
<td>Carter</td>
<td>johncarter#mail.com</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>peterparker#mail.com</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Rambo</td>
<td>johnrambo#mail.com</td>
</tr>
</tbody>
</table>
</td>
</tr>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
and this is the output
Your code is wrong i think
change this
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
to that
<?php endwhile; ?>
</td>
</tr>
</table>
</div>
and try again or loock in this region..
This is my php code.
<?php
session_start();
foreach($_POST AS $key => $val) {
$_SESSION[$key]=$val;
}
mysql_connect('localhost', 'bikec_user', '4348#TxState');
mysql_select_db('bikecats_database');
$cnetid=$_POST['cnetid'];
$cpassword=$_POST['cpassword'];
$cnetid = stripslashes($cnetid);
$cpassword = stripslashes($cpassword);
$cnetid = mysql_real_escape_string($cnetid);
$cpassword = mysql_real_escape_string($cpassword);
$sql="SELECT RentalID, BikeID, RentalStartDate, RentalEndDate
FROM rental
WHERE CustTxStateNetID = '$cnetid'";
$result=mysql_query($sql) OR die(mysql_error());
$row=mysql_fetch_assoc($result);
$rentalid=$row['RentalID'];
$bikeid=$row['BikeID'];
?>
This is the html code. It should be displayihg the query that's been retrieved from the database but for some reason when I run it the table comes up blank. I know I'm only echoing two variables but even those come up empty.
<div class="span9">
<h2>My Account</h2>
<p><strong>My Rentals</strong></p>
<table class="table table-striped">
<thead>
<tr>
<th>Rental ID</th>
<th>Bike ID
<th>Check-Out Date</th>
<th>Return Date</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $rentalid ?></td>
<td><?php echo $bikeid ?></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table><br>
</div><!-- end span -->
Try this:
---------------------------EDITED-----------------------------------------
<?php
session_start();
if(isset($_POST['cnetid'])){
mysql_connect('localhost', 'bikec_user', '4348#TxState');
mysql_select_db('bikecats_database');
$cnetid = mysql_real_escape_string($_POST['cnetid']);
$cpassword = mysql_real_escape_string($_POST['cpassword']);
$table = "<table class='table table-striped' >
<thead>
<tr>
<th>Rental ID</th>
<th>Bike ID
<th>Check-Out Date</th>
<th>Return Date</th>
</tr>
</thead>
<tbody>
";
$sql="SELECT RentalID, BikeID, RentalStartDate, RentalEndDate
FROM rental
WHERE CustTxStateNetID = '$cnetid'";
$body = "";
$result=mysql_query($sql) OR die(mysql_error());
while ($row=mysql_fetch_assoc($result))
{
$body = $body." <tr><td>".$row['RentalID']."</td>
<td>".$row['BikeID']."</td>
<td>".$row['RentalStartDate']."</td>
<td>".$row['RentalEndDate']."</td></tr> ";
}
$table = $table.$body."
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>" ;
}
else
{
$table = "No data..";
}
?>
<div class="span9">
<h2>My Account</h2>
<p><strong>My Rentals</strong></p>
<?php echo $table; ?>
<br>
</div>
PS: Error found, corrected, and code tested xD
Saludos ;)