Pass values from database to user uploaded HTML - php

Updated with current Code*
I thought i would ask for your assistance regarding my issue.
Im wondering how i may go about replacing content that is contained in one column and take the value of another along with its own.
I have the following code and,
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Template</th>
<th>SaleID</th>
<th>Description</th>
<th>Title</th>
<th>Price</th>
<th>Status</th>
<th>User Action</th>
</tr>
</thead>
<tbody>
<?php
$res = $MySQLiconn->query("SELECT * FROM crud WHERE uid='{$_SESSION['userSession']}' ORDER BY id DESC");
while($row=$res->fetch_array())
{
?>
<tr>
<td><!-- THIS COL WILL BE HTML CONTENT A USER UPLOADED ALREADY WHICH SHOULD HAVE THE OTHER VALUES MIXED IN (price, des, title) --></td>
<td><?php echo $row['saleid']; ?></td>
<td><?php echo $row['des']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['price']; ?></td>
<?php
if($row['status']=='Listed')
echo "<td><span class='label label-success'>".$row['status']."</span></td>";
else if($row['status']=='Awaiting')
echo "<td><span class='label label-warning'>".$row['status']."</span></td>";
?>
<td></i> </i> </i></td>
</tr>
<?php
}
?>
</tbody>
</table>

Example:
Model
public function select_data($id) {
$this->db->select(*); //or any fields
$this->db->where('id', $id);
$query = $this->db->get('table_name');
return $query->row_array();
}
Controller:
$data = $this->your_model_name->select_data($id); /id of the specified person
//pass data to the view
$this->load->view('your_view_file', $data);
View:
//display name or description where 'desc' is your filed name from DB
<? echo $data['desc'] ?>
or mix this with your static data:
<p>
My name is <? echo $data['name'] ?>.
</p>
<p>
Description of the person: <? echo $data['desc'] ?>
</p>

Related

How do i hide my table in first time and show after user search something?

i'm trying to build a website where users can find location of my company, but i need to hide the information at the first time and only show the location that users needed.
Here's my code:
<?php
$condition = '';
if(isset($_REQUEST['Kota']) and $_REQUEST['Kota']!=""){
$condition .= ' AND Kota LIKE "%'.$_REQUEST['Kota'].'%" ';
}
if(isset($_REQUEST['Outlet']) and $_REQUEST['Outlet']!=""){
$condition .= ' AND Outlet LIKE "%'.$_REQUEST['Outlet'].'%" ';
}
if(isset($_REQUEST['Alamat']) and $_REQUEST['Alamat']!=""){
$condition .= ' AND Alamat LIKE "%'.$_REQUEST['Alamat'].'%" ';
}
$userData = $db->getAllRecords('lokasi','*',$condition,'ORDER BY id DESC');
?>
<div>
<table class="table table-striped table-bordered">
<thead>
<tr class="bg-primary text-white">
<th>No</th>
<th>Cabang GO</th>
<th>Nama Kota</th>
<th>Alamat Outlet</th>
<th>No Telepon</th>
</tr>
</thead>
<tbody>
<?php
$s = '';
foreach($userData as $val){
$s++;
?>
<tr>
<td><?php echo $s;?></td>
<td><?php echo $val['Kota'];?></td>
<td><?php echo $val['Outlet'];?></td>
<td><?php echo $val['Alamat'];?></td>
<td><?php echo $val['Nomor'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
Start out with <div style=visibility:hidden>. Later change it to 'visible' using JavaScript code.
All the Code you have added is fine, you need few more things to add and it will be all working.
Add HTML Form with an input field for entering searching location and a submit button.
<form action="" method="post">
Search Location: <input type="text" name="location"> <input type="submit" name="submit">`enter code here`
</form>
Add "if" Condition to check the user submitted the form and the result from the db is not empty before the table, where you are displaying all the locations.
<?php if(isset($_POST['submit']) && count($userData) > 0){ ?>
<table class="table table-striped table-bordered">
<thead>
<tr class="bg-primary text-white">
<th>No</th>
<th>Cabang GO</th>
<th>Nama Kota</th>
<th>Alamat Outlet</th>
<th>No Telepon</th>
</tr>
</thead>
<tbody>
<?php
$s = '';
foreach($userData as $val){
$s++;
?>
<tr>
<td><?php echo $s;?></td>
<td><?php echo $val['Kota'];?></td>
<td><?php echo $val['Outlet'];?></td>
<td><?php echo $val['Alamat'];?></td>
<td><?php echo $val['Nomor'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>

How can I make the data that is displayed on the table change with value chosen in a select box?

Based on the user that is chosen within the combo box, I want the table that is displaying user data from the database to only show the data corresponding to the user selected in the combo box.
I mainly tried using an array to store values but I couldn't get that working.
Combo Box that displays the name to pick
<select>
<?php
$res = mysqli_query($db, "SELECT * FROM shifts");
while ($row = mysqli_fetch_array($res))
{
?>
<option><?php echo $row ["name"]; ?></option>
<?php
}
?>
<button class="btn-primary rounded">Find</button>
</select>
</form>
Table that shows the data from the database.
<table class="table table-hover">
<thead class="thead-dark"></thead>
<tr>
<th scope="col">Shift ID</th>
<th scope="col">Name</th>
<th scope="col">Origin</th>
<th scope="col">Destination</th>
<th scope="col">Date</th>
</tr>
</thead>
<?php
global $result;
//Fetch Data form database
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<tbody>
<tr>
<td><?php echo $row['shift_id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['origin']; ?></td>
<td><?php echo $row['destination']; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
</tbody>
</table>
I'm wondering if by using the form and doing a function that on pressing the Find button it looks up the user and displays only it's data. Thanks
Check my code here, there are some thing you must add, like a WHERE statement to your query, when fetching data to only show results with the selected name in the form
<!-- Create a form with a select for all the names -->
<form method="POST" action="">
<select name="name">
<?php
$res = mysqli_query($db, "SELECT * FROM shifts");
while ($row = mysqli_fetch_array($res))
{
?>
<option><?php echo $row ["name"]; ?></option>
<?php
}
?>
<button class="btn-primary rounded" name="find_info">Find</button>
</select>
</form>
<?php
if(isset($_POST['find_info'])){ //If find button is pressed, show this:
?>
<table class="table table-hover">
<thead class="thead-dark"></thead>
<tr>
<th scope="col">Shift ID</th>
<th scope="col">Name</th>
<th scope="col">Origin</th>
<th scope="col">Destination</th>
<th scope="col">Date</th>
</tr>
</thead>
<?php
global $result;
$nameSelected = strip_tags(mysqli_real_escape_string(htmlspecialchars($_POST['name'])));
// Use WHERE in your mysqli query to fetch data where name in database is equal to $nameSelected
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<tbody>
<tr>
<td><?php echo $row['shift_id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['origin']; ?></td>
<td><?php echo $row['destination']; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>

PHP HTML table retrieve multiple data in one <td> tag

I'm trying to generate a list from database in a HTML table just like image below;
https://i.stack.imgur.com/61XLl.png
And here's what i did;
https://i.stack.imgur.com/lLsvF.png
And the code;
<table cellpadding="3" border="1" style="width:100%;margin-top:30px; margin-bottom:50px; font-size:12px">
<thead>
<tr>
<th>KURSUS</th>
<th rowspan="2">NAMA PENSYARAH</th>
<th rowspan="2">NO. SIRI</th>
</tr>
<tr>
<th>NAMA</th>
</tr>
</thead>
<tbody align="center">
<?php
if($numrow>0)
{
while($row = $select->fetch_assoc()){
$code=explode("/",$row['po_code']);
$list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
?>
<tr>
<td><?php echo $row['po_name']; ?></td>
<?php while($rowlist = $list->fetch_assoc()){
$name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
$rowname=$name->fetch_array();?>
<td><?php echo $rowname['user_name']; ?></td>
<td><?php echo $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']; ?></td>
<?php } ?>
</tr>
<?php
}
}
?>
</tbody>
</table>
Help me. Thank you in advance :)
Use this code. Concat user names and code with "br" tags in the second while loop and display them in "tds" after while loop.
<tbody align="center">
<?php
if($numrow>0)
{
while($row = $select->fetch_assoc()){
$code=explode("/",$row['po_code']);
$list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
?>
<tr>
<td><?php echo $row['po_name']; ?></td>
<?php
$user_names = $codes = ''; // define empty variables
while($rowlist = $list->fetch_assoc()){
$name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
$rowname=$name->fetch_array();
$user_names .= $rowname['user_name']."<br/>"; //concat to a single string
$codes .= $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']."<br/>"; //concat to a single string
}?>
<td><?php echo $user_names;?></td>
<td><?php echo $codes;?></td>
</tr>
<?php
}
}
?>
</tbody>
Put the <td> outside the <?php while($rowlist = $list->fetch_assoc()){
Or get all your data before you start display html and store it in a multi-dimensional array. Then simply loop through the data array. That way you won't have as much php mixed with html also.

how to create link for one user id from datatable and view only that user id detail in next page?

I have a function manage_child which it is in a datatables that show all children details. My problem is I want to view only one child details at another pages. I have tried to implement this example of code in my function but it didnt work since I call it like this:
`<td><a href="?pages=child_view"</a><?php_e($child_id); ?></td>`
I have link the page where the details should go but it didn't work. I am still in my basic using php and I use mysqli for the database.
This is the example that I tried to implement in my code but still doesn't work.
echo "<td>".$row[id]."</td>";
echo "<td>".$row[l_name]."</td>";
echo "<td>".$row[f_name]."</td>";
echo "<td>".$row[ssn]."</td>";
this is my code
function manage_child(){
$query = query("SELECT * FROM register_child");
confirm($query);
?>
<div class='container-fluid'>
<div class='row'>
<h1 class="page-header">Children
<small>Information</small>
</h1>
<div class="panel-body">
<div class="dataTable_wrapper">
<table width="50%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>No</th>
<th>CHild ID</th>
<th>Child IC</th>
<th>Name</th>
<th>Address</th>
<th>Mother name</th>
</tr>
</thead>
<tbody>
<?php
$bil = 0;
while($row = fetch_array($query)) {
$password = $row['ic_no'];
$child_id = $row['child_id'];
$child_name = $row['child_name'];
$address = $row['address'];
$mother_name = $row['mother_name'];
$bil++;
?>
<tr class="odd gradeX">
<td><?php echo $bil; ?></td>
<td><?php _e($password);?></td>
<td><a href="?pages=child_view"</a><?php _e($child_id); ?></td>
<td><?php _e($child_name); ?></td>
<td><?php _e($address); ?></td>
<td><?php _e($mother_name); ?></td>
<?php
}// End of while
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php
}

No data echoed inside html table

Var dump result
I am fetching data in MySQL into an HTML table:
<div id="toggleDiv" class="">
<div class="box-body" id="toggleDiv_2">
<div class="row">
<div class="col-md-6">
<?php
$select_patient_info_cash =
"SELECT * FROM patient_info WHERE id_logged = :id_logged".
" AND patient_id = :patient_id AND payment_type = :pt";
$select_patient_info_cash_stmt =
$conn->prepare($select_patient_info_cash);
$select_patient_info_cash_stmt->bindValue(":id_logged", $id_logged);
$select_patient_info_cash_stmt->bindValue(":patient_id", $patient_id);
$select_patient_info_cash_stmt->bindValue(":pt", "cash");
$select_patient_info_cash_stmt->execute();
$select_patient_info_cash_stmt->fetch(PDO::FETCH_ASSOC);
$select_patient_info_cash_stmt_count =
$select_patient_info_cash_stmt->rowCount();
if($select_patient_info_cash_stmt_count > 0){ ?>
<table style="text-align:center"
class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Project Description</th>
<th>Project Cost</th>
<th>Date Of Pay</th>
</tr>
</thead>
<?php foreach ($select_patient_info_cash_stmt as $cash) { ?>
<tr>
<td><?php echo $cash['project'] ?></td>
<td><?php echo $cash['project_cost'] ?></td>
<td><?php echo $cash['date_now'] ?></td>
</tr>
<?php } ?>
</table>
<?php } else { ?>
<?php } ?>
</div><!-- /.col -->
Now I test it for a user that have data in patient info where payment_type != cash, and the <thead> didn't show up. But when I test it where payment_type=cash the <thead> shows up but no data are echoed into line.
It should show me 2 new lines after <thead> and I can't figure out why data are not displayed on the page
I think you miss ->fetch() from your prepared statement. According to PHP docs:
<?php
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array($_GET['name']))) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}
?>
Therefore you need to modify your code to something like:
<?php while ($cash = $select_patient_info_cash_stmt->fetch()) { ?>
<tr>
<td><?php echo $cash['project'] ?></td>
<td><?php echo $cash['project_cost'] ?></td>
<td><?php echo $cash['date_now'] ?></td>
</tr>
<?php } ?>
I also suggest that you should use a MVC framework or some sort of template engines. Mixing PHP and HTML is a very bad practice.
References: http://php.net/manual/en/pdo.prepared-statements.php

Categories