displaying results from a mysql database - php

Working on a project to display a list of cars
$username="username";
$password="password";
$database="listofcars";
$mysqli = new mysqli("localhost", $username, $password, $database);
#mysql_select_db($database) or die( "Unable to select database");
$query2="SELECT * FROM cars";
$result=$mysqli->query($query2);
$num=$mysqli->mysqli_num_rows($result);
$mysqli->close();
echo "
<div class="item">
<div class="container">
<div class="imgcontainer"><img alt="Cars for sale" src="$carimage" width="380" height="380" /></div>
<div class="details">
<a href="$internallink" target="_blank">
<h3 class="title"> $carname <br />
<span> $cartype </span></h3>
<p>
$cardesc
</p>
<div class="button"><span data-hover="Order Car">Order Car</span></div>
</a>
</div>
</div>
</div>
I need it to loop the results
database name: listofcars
tablename: cars
Field names
carsid
carname
carimage
cartype
internallink
cardesc

Try this to see if it works.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "listofcars";
// Create connection
$mysqli = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$mysqli) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM cars";
$result = mysqli_query($mysqli, $sql);
?>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<div class="item">
<div class="container">
<div class="imgcontainer">
<img alt="Cars for sale" src="<?php echo $row['carimage']; ?>" width="380" height="380">
</div>
<div class="details">
<a href="<?php echo $row['internallink']; ?>" target="_blank">
<h3 class="title"><?php echo $row['carname']; ?><br><span><?php echo $row['cartype']; ?></span></h3>
<p><?php echo $row['cardesc']; ?></p>
<div class="button">
<span data-hover="Order Car">Order Car</span>
</div>
</a>
</div>
</div>
</div>
<?php } ?>
mysqli_close($mysqli);

You need to leave the connection open until you are done and loop through it after running the query.
Something like
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "listofcars";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Query
$sql = "SELECT * FROM cars";
$result = mysqli_query ($conn, $sql);
while($row = mysqli_fetch_array($result)) {
//Whatever you want to do for each row...
echo "
<div class='item'>
<div class='container'>
<div class='imgcontainer'><img alt='Cars for sale' src='".$row[carimage]."' width='380' height='380' /></div>
<div class='details'>
<a href='".$row[internallink]."' target='_blank'>
<h3 class='title'> ".$row[carname]." <br />
<span> ".$row[cartype]." </span></h3>
<p>
".$row[cardesc]."
</p>
<div class='button'><span data-hover='Order Car'>Order Car</span></div>
</a>
</div>
</div>
</div>";
}
$conn->close();

Related

why some of my html content is getting removed after adding some php code?

after adding some PHP code the code below that is getting removed. if I remove the PHP part then it is working fine. even in the source code the below HTML code is not showing
<div class="row data">
<div class="row info">
<div class="datatitle">Education</div>
</div>
<div class="grad-fetch row">
<?php
session_start();
$hostname = "127.0.0.1";
$username = "root";
$db_password = "";
$db_name = "resume";
$conn = mysqli_connect($hostname, $username, $db_password, $db_name);
if(!$conn){
die("connection failed : ".mysqli_connect_error());
}
/* $sql = graduation(degree_status ,college ,start_year ,end_year ,degree ,stream ,performence_scale ,performence_marks)*/
$sql = 'SELECT * FROM graduation ';
$result = mysqli_query($conn, $sql);
if (!$result) {
die("Error: " . $sql . "<br>" . mysqli_error($conn));
}
while ($row=mysqli_fetch_array($result)) {
$deg= $row['degree'];
$str= $row['stream'];
$str_yr= $row['start_year'];
$end_yr= $row['end_year'];
$clg= $row['college'];
$prf_scl= $row['performence_scale'];
$prf_mrk= $row['performence_marks'];
}
?>
</div class="row">
<h5>
<?php
echo $deg;
echo $str;
echo $str_yr;
echo $end_yr;
?>
</h5>
<div>
<?php
echo $clg;
?>
</div>
<div>
<?php
echo $prf_scl;
echo $prf_mrk;
?>
</div>
<div>
<?php
exit;
mysqli_close($conn);
?>
</div>
<div class="row dialouge"><h4>Tell us bit about your education</h4></div>
<div class="col userinfo">
<div class="row discipline" data-toggle="modal" data-target="#graduation-modal" id="grad">Graduation
<i class="fa fa-plus education-fa-plus fa-x" aria-hidden="true"></i>
</div>
<div class="row discipline" data-toggle="modal" data-target="#hs-modal">XII(Higer Secondary)
<i class="fa fa-plus education-fa-plus fa-x" aria-hidden="true"></i>
</div>
<div class="button-container">
PREVIOUS
</div>
</div>
if I'm not adding the PHP code then the all the contents are showing but after adding the below part is not showing. I don't understand what is wrong here
<?php
exit;
mysqli_close($conn);
?>
You need to remove exit from your code as it will stop the execution right there.

Display the value on to the label below the applicant pending

I have to pull out the value from the database table call count.php which I already got the value 49. The problem is how to insert into the html
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hopeplace";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT COUNT(*) AS TOTAL_APPLICANT FROM applicant";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo $row["TOTAL_APPLICANT"];
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
code for the container for the applicant pending
<div class="content">
<div class="row">
<div class="col-xs-5">
<div class="icon-big icon-danger text-center">
<i class="ti-user"></i>
</div>
</div>
<div class="col-xs-7">
<div class="numbers">
<p>Applicant Pending</p>
<p>*this is the place where value need to be put*</p>
</div>
</div>
</div>
<div class="footer">
<hr />
</div>
</div>
You can assign value to one variable and echo that variable inside html
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hopeplace";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$count = 0;
$sql = "SELECT COUNT(*) AS TOTAL_APPLICANT FROM applicant";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$count = $row["TOTAL_APPLICANT"] ;
}
}
mysqli_close($conn);
?>
code for the container for the applicant pending will be like
<div class="content">
<div class="row">
<div class="col-xs-5">
<div class="icon-big icon-danger text-center">
<i class="ti-user"></i>
</div>
</div>
<div class="col-xs-7">
<div class="numbers">
<p>Applicant Pending</p>
<p>
<?php echo $count; ?>
</p>
</div>
</div>
</div>
<div class="footer">
<hr />
</div>
</div>
save both html & php in same file or ensure that your html code is saved as a .php file. So, your script will be - <p><?php echo $total; ?></p> or <p><?= $total ?></p>

MySQL Database row in a Button

I have a database for my ToDo App which has following cloumns:
| ID | ShortDescription | Description | Date | Status |
I already can add a Task to the Datatable and can see it in phphmyadmin.
I have following code till now:
$id = mysql_real_escape_string($_GET['id']);
$out = 'SELECT * FROM ToDo1 WHERE `id` = '.$id.' LIMIT 1';
$result = mysqli_query($link, $out);
$row= mysqli_fetch_array($result);
?>
<div id= "OutShortDescription">
<?php
echo $row['ShortDescription'];
?>
</div>
<div id= "OutDescription">
<?php
echo $row['Description'];
?>
</div>
<div id= "OutDate">
<?php
echo $row['Date'];
?>
</div>
<div id= "OutStatus">
<?php
echo $row['Status'];
?>
</div>
Now I want to put every ID row on a own Site.
For that I want to make a table of Buttons (Buttonnumber=ID).
On this Button should only be shown the ShortDescription and when I click it I want to go to a the Site which matches to the Button.
Can someone help me?
EDIT
okay thanks now I have this code but it wont work:
<?php
$dbname= 'Groups';
$dsn = 'mysql:host=localhost;dbname='.$dbname;
$user = 'root';
$pass = '';
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM groups2 WHERE id = :id LIMIT 1";
$ps = $db->prepare($query);
$ps->bindParam(':id', $id);
$ps->execute();
$row = $ps->fetch(PDO::FETCH_ASSOC);
?>
<div class="searchwindow">
<?php
$data = $link->query('SELECT * FROM Groups2');
foreach($data as $row) {
echo '<p><input type="button" onclick="window.location = All_Groups.php?id=' . $row['ID'] . ' value='.$row['ShortDescription'].' /></p>';
}
I have now following code
<div data-role="page" id="SearchPage" data-title="SearchPage">
<div data-role="header">
<h1>Search</h1>
</div>
<div data-role="content">
<div data-role="header">
<form>
<fieldset data-role="controlgroup" data-type="horizontal" data-mini="true" align="center" id="selectMenu">
<select name="selectStatus" id="selectStatus">
<option value="0">Status</option>
<option value="1">Done</option>
<option value="2">In Progress</option>
</select>
</fieldset>
</form>
</div>
<?php
$dbname= 'Groups';
$dsn = 'mysql:host=localhost;dbname='.$dbname;
$user = 'root';
$pass = '';
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM groups2 WHERE id = :id LIMIT 1";
$ps = $db->prepare($query);
$ps->bindParam(':id', $id);
$ps->execute();
$row = $ps->fetch(PDO::FETCH_ASSOC);
?>
<div class="searchwindow">
<?php
$data = $link->query('SELECT * FROM Groups2');
foreach($data as $row) {
$path = $row['ID'];
$description = $row['ShortDescription'];
echo ("<form action='All_Groups.php?id=$path'><button type='submit' value='$description'/>$description</form>" );
}
?>
</div>
</div>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li>Search</li>
<li>New</li>
<li>More</li>
</ul>
</div><!-- Ende navbar -->
</div><!-- Ende footer -->
</div>
And this is my All_groups.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Unbenanntes Dokument</title>
</head>
<body>
<?php
$servername ="localhost";
$username = "root";
$password = "";
$dbname = "Groups";
$link = mysqli_connect($servername, $username, $password, $dbname);
if (!$link) {
die('Verbindung nicht möglich : ' . mysqli_error($link) );
}
?>
<?php
$row="";
$Date="";
$Status="";
$ShortDescription="";
$Description="";
mysqli_select_db($link, "groups");
?>
</div>
<?php
$id = mysql_real_escape_string($_GET['id']);
$out = "SELECT * FROM groups2 WHERE ID = '$id' ";
$result = mysqli_query($link, $out);
$id = mysqli_fetch_array($result);
?>
<div id= "OutShortDescription">
<?php
echo $id['ShortDescription'];
?>
</div>
<div id= "OutDescription">
<?php
echo $id['Description'];
?>
</div>
<div id= "OutStatus">
<?php
echo $id['Status'];
?>
</div>
<div id= "OutDate">
<?php
echo $id['Date'];
?>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li>Search</li>
<li>New</li>
<li>More</li>
</ul>
</div><!-- Ende navbar -->
</div>
</body>
</body>
</html>
First of all, don't use the mysql_* methods! Use PDO or mysqli_* instead.
Below, I'm pasting your example query, using PDO:
$dsn = 'mysql:host=localhost;dbname='.$dbname;//$dbName is the name of your database
$user = 'root';
$pass = '123';//use your login information here
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM ToDo1 WHERE id = :id LIMIT 1";
$ps = $db->prepare($query);
$ps->bindParam(':id', $id)
$ps->execute();
$row = $ps->fetch(PDO::FETCH_ASSOC);
Now, to get your button, you don't need to use jquery:
<?php
$path = $row['ID'];
$description = $row['ShortDescription'];
echo "<form action='your/site/$path'><button type='submit' value='$description'/>$description</form>"
?>
Another option is use the onclick:
<?php
$path = $row['ID'];
$description = $row['ShortDescription'];
echo "<input type=\"button\" onclick=\"location.href='your/site/$path'\" value=\"$description\" />";
?>
The \ before " is a escape, so PHP will print the character " and not interpret it as the end of your string.
Advice: Try to avoid mix HTML and PHP, in general this is a bad practice.

Putting the WHERE statement in SQL STATEMENT

<div class = "col-md-9 text-left">
<?php
$host = 'localhost';
$dbname = 'project';
$username = 'root';
$password = '1234';
$charset = 'utf8';
try
{
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = "SELECT subject,description,time,date FROM status";
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
$usid = ($row['userID']);
$sql1 = 'SELECT status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname
FROM status , tbl_users
WHERE status.userID=tbl_users.userID ORDER BY status.time DESC';
$q1 = $pdo->prepare($sql1);
$q1->execute([$usid]);
$q1->setFetchMode(PDO::FETCH_ASSOC);
}
catch (PDOException $e)
{
die("Could not connect to the database $dbname :" . $e->getMessage());
}
?>
<?php while ($row = $q->fetch()): ?>
<?php while ($row1 = $q1->fetch()): ?>
<div class="col-md-9">
<div class="box box-widget">
<div class="box-header with-border">
<div class="user-block">
<img class="img-circle" src="<?php echo $row10['des']; ?><?php echo $row9['userPic']; ?>" alt="User Image">
<span class="username"><?php echo htmlspecialchars($row1['Fname']); ?> <?php echo htmlspecialchars($row1['Lname']); ?></span>
<span class="description">Shared publicly - <?php echo htmlspecialchars($row['time']) ?> <?php echo htmlspecialchars($row['date']) ?></span>
</div>
<!-- /.user-block -->
<div class="box-tools">
<button type="button" class="btn btn-box-tool" data-toggle="tooltip" title="Mark as read">
<i class="fa fa-circle-o"></i>
</button>
<button type="button" class="btn btn-box-tool" data-widget="collapse">
<i class="fa fa-minus"></i>
</button>
<button type="button" class="btn btn-box-tool" data-widget="remove">
<i class="fa fa-times"></i>
</button>
</div>
<!-- /.box-tools -->
</div>
<!-- /.box-header -->
<div class="box-body">
<p><b><?php echo htmlspecialchars($row1['subject']) ?></b></p>
<p><i><?php echo htmlspecialchars($row1['description']) ?></i></p>
<?php
// Check connection
$servername = "localhost";
$username = "root";
$password = "1234";
$dbname = "project";
htmlspecialchars($a = $row1['stno']);
$d1 = $row7['userID'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM likes WHERE rec = $a";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
$GLOBALS['a'] = $row['do'];
}
}
$z4 = $GLOBALS['a'];
if ($d1==$z4)
{
include ("unlikee.php");
}
else
{
include ("likee.php");
}
$conn->close();
?>
<span class="pull-right text-muted"><?php
$con=mysqli_connect("localhost","root","1234","project");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
htmlspecialchars($a = $row1['stno']);
$sql="SELECT * FROM likes WHERE rec = $a";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
printf("%d\n",$rowcount);
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
</h5>
<span class="description-text"><?php
$con=mysqli_connect("localhost","root","1234","project");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
htmlspecialchars($a = $row1['stno']);
$sql="SELECT * FROM likes WHERE rec = $a";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
if ($rowcount==1)
echo 'Like';
else
echo 'Likes';
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?> - 3 comments</span>
</div>
<!-- /.box-body -->
<!-- /.box-footer -->
<div class="box-footer">
<form action="#" method="post">
<img class="img-responsive img-circle img-sm" src="../dist/img/user4-128x128.jpg" alt="Alt Text">
<!-- .img-push is used to add margin to elements next to floating images -->
</form>
</div>
<!-- /.box-footer -->
</div>
<!-- /.box -->
</div>
<?php endwhile; ?> <?php endwhile; ?>
</div>
I want to retrieve the Data of only one USER but I don't know how to give a condition for it in SQL Statement. Where and how I can put WHERE userID = $user_Session?
$sql = "SELECT subject,description,time,date FROM status";
In the two code statements above where should I put the first?
$sql1 = 'SELECT status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname
FROM status , tbl_users
WHERE status.userID=tbl_users.userID ORDER BY status.time DESC';
$sql1 =
'SELECT
status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname
FROM
status , tbl_users
WHERE
status.userID=tbl_users.userID
AND [correct_table_name].userID = $user_Session # here with AND instead WHERE
ORDER BY
status.time DESC';
Here is the code I added a parameter UID
try
{
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = "SELECT subject,description,time,date FROM status";
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
$usid = ($row['userID']);
$sql1 = 'SELECT status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname
FROM status , tbl_users
WHERE status.userID=tbl_users.userID and tbl_users.userID = :UID ORDER BY status.time DESC';
$q1 = $pdo->prepare($sql1);
$q1->bindParam(':UID', $usid, PDO::PARAM_INT); //call with param
$q1->execute();
$q1->setFetchMode(PDO::FETCH_ASSOC);
}

Is there a way to display the results from the data in a loop that prints <ul> & <li>?

<?php
$servername = "localhost";
$username = "khcy4dau_kesh";
$password = "";
$dbname = "khcy4dau";
//Create Connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check Connection
if ($conn->connect_error) {
die("Connection Failed : " . $conn->connect_error);
}
?>
</head>
<body>
<div class="container">
<div id="slideshow" class="slideshow">
<ul>
<li>
<div class="slide">
//How do I loop the data from the database so that it prints all instead of one by one//
<img class="icon" src="img/icons/heart.svg" alt="Heart Icon"/>
<blockquote>
<?
$sql = "SELECT quotations from quote";
if ($result = mysqli_query($conn,$sql))
{
$row = mysqli_fetch_row($result);
echo " $row['quotations'] ";
mysqli_free_result($result);
}
?>
</blockquote>
<?
echo "<p> $author </p>";
?>
</div>
</li>
<li>
<div class="slide">
<img class="icon" src="img/icons/letter.svg" alt="Letter Icon"/>
<blockquote>
<p>If you don't know where you are going, any road will get you
there.</p>
</blockquote>
<p>Lewis Carroll</p>
</div>
</li>
</ul>
</div>
</div><!-- /container -->
</body>
</html>
<?
$conn->close();
?>
You are probably looking for this.. but it's not particularly clear ..
I've assumed Author is in your quote table, and I have no idea what determines what Icon you use, so I was creative.. Decide what to use in the allocation from the query..
<?php
$servername = "localhost";
$username = "khcy4dau_kesh";
$password = "";
$dbname = "khcy4dau";
//Create Connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check Connection
if ($conn->connect_error) {
die("Connection Failed : " . $conn->connect_error);
}
$uberQuotes= array();
$sql = "SELECT quotations, author from quote";
if ($result = mysqli_query($conn,$sql))
{
while ($row = mysqli_fetch_row($result))
{
$uberQuotes['author'][] = array('quote'=>$row['quotations'], 'icon' => 'heart');
}
mysqli_free_result($result);
}
$conn->close();
if ($uberQuotes)
{
$quotesHTML = '<ul>';
foreach ($uberQuotes as $author => $quotes)
{
foreach ($quotes as $quote)
{
$quotesHTML .= '
<li>
<div class="slide">
<img class="icon" src="img/icons/'.$quote['icon'].'.svg" alt="'.ucFirst($quote['icon']).' Icon"/>
<blockquote>
'.$quote['quote'].'
</blockquote>
<p> '.$author.' </p>
</div>
</li>';
}
}
$quotesHTML .= '</ul>';
}
else
{
$quotesHTML = 'No Quotes Found';
}
?>
</head>
<body>
<div class="container">
<div id="slideshow" class="slideshow">
<?=$quotesHTML?>
</div>
</div><!-- /container -->
</body>
</html>
You can use any looping statements, gave you example foreach loop, fetch data into $items and loop the array:
<?php foreach($items as $item): ?>
<li><div class="slide"></div></li>
<?php foreach; ?>
Try something like this
<ul>
<?php foreach($items as $item): ?>
<li><div class="slide"></div></li>
<?php foreach; ?>
</ul>

Categories