while loop showing only first result - php

<div class="row">
<?php
require 'config.php';
$query = "SELECT * FROM `posts` ORDER BY `id` DESC LIMIT 8";
$do = mysqli_query($con, $query) ;
while ($result = mysqli_fetch_array($do)){
$query="SELECT * FROM `lang` where id='$result[langid]'";
$do=mysqli_query($con,$query);
$lang=mysqli_fetch_array($do);
$query="SELECT * FROM `users` where id='$result[userid]'";
$do=mysqli_query($con,$query);
$user=mysqli_fetch_array($do);
$String=$result['titel'];
$text_length = 30; // طول الحروف المراد ظهورها
if (strlen($String) > $text_length) {
// اذا كان طول النص اكبر من النص المراد ظهورة نفذ التالي
$String = substr($String, 0, 33);
// قص من النص التالي ما بعد الرقم في المتغير الخاص بطول الحروف المراد ظهورها
$String .= " ...";
// اضف إلى النص بعد ارجاعه هذه النقاط
}
?>
<div class="col-md-3" >
<div class="panel panel-default ">
<!-- Default panel contents -->
<div class="panel-heading" >
<?php
echo $String;
?>
</div>
<div class="panel-body">
<p><span class="glyphicon glyphicon-user"></span><b> <?php echo $user['username']; ?></b> </p>
<p> <span class="glyphicon glyphicon-folder-open"></span><b> <?php echo $lang['name']; ?></b></p>
</div>
<a href="show<?php echo $result['id']; ?>"><div class="panel-footer text-center ">
<b>عرض التحدي</b>
</div></a>
</div>
</div>
<?php } ?>
</div>
Hi this is the code it's only showing the first result in while loop I want 8 results.
I have 3 tables from MySQL and 3 querys I tried every thing I could but nothing changed.
Any help?
It's PHP 5.4 and mysqli class
Update: now the code works
<div class="row">
<?php
require 'config.php';
$query = "SELECT * FROM `posts` ORDER BY `id` DESC LIMIT 8";
$do = mysqli_query($con, $query) ;
while ($result = mysqli_fetch_assoc($do)){
$query="SELECT * FROM `lang` where id='$result[langid]'";
$do1=mysqli_query($con,$query);
$query="SELECT * FROM `users` where id='$result[userid]'";
$do2=mysqli_query($con,$query);
$String=$result['titel'];
$text_length = 30; // طول الحروف المراد ظهورها
if (strlen($String) > $text_length) {
// اذا كان طول النص اكبر من النص المراد ظهورة نفذ التالي
$String = substr($String, 0, 33);
// قص من النص التالي ما بعد الرقم في المتغير الخاص بطول الحروف المراد ظهورها
$String .= " ...";
// اضف إلى النص بعد ارجاعه هذه النقاط
}
?>
<div class="col-md-3" >
<div class="panel panel-default ">
<!-- Default panel contents -->
<div class="panel-heading" >
<?php
while ( $user=mysqli_fetch_assoc($do2) AND $lang=mysqli_fetch_assoc($do1) ){
echo $String;
?>
</div>
<div class="panel-body">
<p><span class="glyphicon glyphicon-user"></span><b> <?php echo $user['username']; ?></b> </p>
<p> <span class="glyphicon glyphicon-folder-open"></span><b> <?php echo $lang['name']; ?></b></p>
</div>
<a href="show<?php echo $result['id']; ?>"><div class="panel-footer text-center ">
<b>عرض التحدي</b>
</div></a>
</div>
</div>
<?php }} ?>
</div>

You are doing several queries, but looping only on the last one which is SELECT * FROM 'users' where id='$result[userid]', and it sounds like this query is going to give you a single result. (A single user)
You can either store the results of each queries, and use them all in a single loop later on, or join your tables to get all the data you want from a single query.

Related

How Insert PHP syntax on if conditional with html code inside

So i want to place this code below
$keyword="";
if (isset($_POST['searchpakaian'])) {
$keyword = $_POST['searchpakaian'];
}
$query = mysqli_query($conn,"SELECT * FROM tbl_pakaian WHERE nama_pakaian LIKE '%".$keyword."%' ORDER BY id ASC limit 0,6");
$hitung_data = mysqli_num_rows($query);
if ($hitung_data > 0) {
while ($row = mysqli_fetch_array($query)) {
?>
<div class="col">
<div class="card h-100">
<div class="card-img-wrap ">
<img src="img/Galeri/pakaian/<?php echo $row['gambar_pakaian'] ?>" class="card-img-top" alt="...">
</div>
<div class="card-body">
<h5 class="card-title"><?php echo $row['nama_pakaian'] ?></h5>
<p class="card-text "><?php echo $row['keterangan_pakaian'] ?></p>
</div>
</div>
</div>
<?php } } else { ?>
<center><h4>Tidak Ada Data</h4></center>
<?php } ?>
On if conditional statement
if($req == 'something'){
}
Here is my full code
<?php
include '../koneksi.php';
$req = $_REQUEST['req'];
$keyword="";
if (isset($_POST['searchpakaian'])) {
$keyword = $_POST['searchpakaian'];
}
$query = mysqli_query($conn,"SELECT * FROM tbl_pakaian WHERE nama_pakaian LIKE '%".$keyword."%' ORDER BY id ASC limit 0,6");
$hitung_data = mysqli_num_rows($query);
if ($hitung_data > 0) {
while ($row = mysqli_fetch_array($query)) {
?>
<div class="col">
<div class="card h-100">
<div class="card-img-wrap ">
<img src="img/Galeri/pakaian/<?php echo $row['gambar_pakaian'] ?>" class="card-img-top" alt="...">
</div>
<div class="card-body">
<h5 class="card-title"><?php echo $row['nama_pakaian'] ?></h5>
<p class="card-text "><?php echo $row['keterangan_pakaian'] ?></p>
</div>
</div>
</div>
<?php } } else { ?>
<center><h4>Tidak Ada Data</h4></center>
<?php } ?>
If i just copy the code to the if conditional statement code, it just say 'unexpected end of file'
What should i do?
The syntax error are vey simple to find just confirm that when you copy inside the if, dont repeat php keys as and the echo finish it with ;
<?php echo('hello world'); ?>
OR the same echo but simple deleting the php tag by = simbol (only work with echo)
<?= 'hello world' ?>

Blank page when entering space in php search

When adding a name with a space in the search bar the header and the footer remains however the body disappears.
I even added ini_set('display_errors', '1') and ini_set( 'default_charset', 'UTF-8' ); to check for errors however that didn't help.
Please note that the function works fine except when when entering any spaces
Here is the code:
<?php
//ini_set('display_errors', '1');
//ini_set( 'default_charset', 'UTF-8' );
session_start();
$title="Search";
include('includes/connection.php');
$url=parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);
$url_array=explode("/",$url);
$name=end($url_array);
$uname = urldecode($name);
$position="absolute";
$sql ="SELECT userName , firstName, lastName,id
FROM users
WHERE userName LIKE ?
or firstName like ?
or lastName Like ?
or concat(firstName,' ',lastName) = ?";
$params=array("$uname","$uname","$uname","$uname");
$query= $dbh -> prepare($sql);
if($query-> execute($params)){
$results=$query->fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() > 6) {
$position="inherit";
} else {
$position="absolute";
}
}
if (!$results) {
$mess = true;
// echo '<script type="text/javascript">alert("Hi");</script>';
}
//else {
//$mess = true;
//$query->errorInfo();
//}
include('includes/header.php');
//else
//{
// $mess = $query->errorInfo();
// echo '<script type="text/javascript">alert("'.$mess[2].'");</script>';
//}
?>
<main role="main" class="mbr-fullscreen" style="display: grid;position: <?php echo $position;?>;">
<div class="album py-5" >
<div class="container">
<div class="row">
<?php
if(!$results) {
echo'<div class="col-lg-12 mx-auto">
<center>
<h1 style="padding: 10px; color: #575957;">No results found</h1>
<h6>We couldn\'t find any user matching your search</h6>
<a href="/index.php" class="btn btn-primary display-4" >Search Again</a>
</center>
</div> ';
}
?>
<?php
foreach($results as $result) {
?>
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<div class="card-body">
<h3 class="card-text text-center" style="text-transform: capitalize;"><?php echo htmlentities($result->userName);?></h3>
<p class="text-center"> <?php echo htmlentities ($result->firstName).' '; echo htmlentities ($result->lastName); ?> </p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group mx-auto mbr-form">
View
</div>
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</div>
</main>
<?php include('includes/footer.php');?>
Like statement does not work like that. You should change your query in the proper format.
Replace:
$params=array("$uname","$uname","$uname","$uname");
With:
$params=array("%$uname%","%$uname%","%$uname%","%$uname%");
1. Add $uname=str_replace(" ", "% %", $uname); after $uname = urldecode($name); to wrap each space with %
2. Replace concat(firstName,' ',lastName) = ? With concat(firstName,' ',lastName) like ?
3. Follow Tahasin instructions, Replace $params=array("$uname","$uname","$uname","$uname"); with $params=array("%$uname%","%$uname%","%$uname%","%$uname%");
I solved the problem by adding the following line in the home page
search = search.replace(/\s/g,"-");
And in my search page, I added the following code
$name=str_replace('-',' ',$uname);
You people are great, thank you for helping me out

The page already appear but the article still on the same page

I want to limit my articles to 3 per page ... the page has appeared but the article is still on one page .
require_once 'koneksi.php';
$result = mysqli_query($koneksi, "SELECT * FROM tbl_artikel");
$aktif = 'artikel';
$halaman = 3;
$page = isset($_GET["halaman"]) ? (int)$_GET["halaman"] : 1;
$mulai = ($page>1) ? ($page * $halaman) - $halaman : 0;
$total = mysqli_num_rows($result);
$pages = ceil($total/$halaman);
$query = "SELECT * FROM tbl_artikel LIMIT $mulai, $halaman";
$result2 = mysqli_query($koneksi, $query);
$no =$mulai+1;
?>
and here is the code for article
<?php while($artikel = mysqli_fetch_assoc($result)) : ?>
<div class="artikel">
<div class="info">
<div class="post mb-2">
<span class="tgl"><?= date('d M y', strtotime($artikel['tanggal'])) ?></span>
<span class="judul"><?= $artikel['judul'] ?></span>
</div>
</div>
<div class="detail">
<div class="clearfix" style="text-align: justify;">
<img src="images/artikel/<?= $artikel['foto'] ?>" alt="" width="200px" class="float-left mr-2">
<?= substr($artikel['isi'], 0, 150) . '...' ?>
</div>

Fetch an image from Mysql in html with PHP

This is my code and I wanted to display an image from DB. I followed some videos and recommendations here on Stack Overflow, but still does not work.
Other values like description, title etc works, so the problem is not in the connection with the DB.
Thank you.
<?php
$sql= "SELECT * FROM guides WHERE id='$id'";
$result = mysqli_query($link, $sql);
$num = mysqli_num_rows($result);
if ($num > 0) {
$row = mysqli_fetch_assoc($result)
?>
<h1 class="title"><?=$row['title']?></h1>
<?php echo' <img class="header" style="height:400px" src="data:image/jpeg;base64,'.base64_encode( stripslashes($row['photo']) ).'" style="width:100px;"></img>' ?>
<div class="row">
<div class="leftcolumn">
<h2><?=$row['date']?></h2>
<div class="card" >
<div class="clearfix">
<p class="text" ><?=$row['description']?></p>
</div>
</div>
</div>
</div>
<?php
}
?>
I think you should try and specify the folder containing the given image also. E.g src = “containing_folder/$row[‘photo’]” and see if that works. You can still add your functions to the $row[‘photo’], this was just an example.
mysqli_fetch_assoc($result) this returns an array and you need to loop through the array:
The modified code given below----
$sql= "SELECT * FROM guides WHERE id='$id'";
$result = mysqli_query($link, $sql);
$num = mysqli_num_rows($result);
if ($num > 0) {
while($row = mysqli_fetch_assoc($result)){
?>
<h1 class="title"><?=$row['title']?></h1>
<?php echo' <img class="header" style="height:400px" src="data:image/jpeg;base64,'.base64_encode( stripslashes($row['photo']) ).'" style="width:100px;"></img>' ?>
<div class="row">
<div class="leftcolumn">
<h2><?=$row['date']?></h2>
<div class="card" >
<div class="clearfix">
<p class="text" ><?=$row['description']?></p>
</div>
</div>
</div>
</div>
<?php
}
}
?>

My PHP search displays ALL entries on database even if only one is correct

Im a PHP newbie. I am creating a jobs website and my search function tells me when there is no result but if there is, it displays ALL the jobs I have entered on the database. Please assist, I have tried everything.
here is my code:
<?php
if(isset($_POST['submit']))
{
$search = $_POST['keyword'];
$query = "SELECT * FROM jobs WHERE job_tags LIKE '%$search%'";
$search_query = mysqli_query($connection, $query);
if(!$search_query) {
die ("query failed" . mysqli_error($connection));
}
$count = mysqli_num_rows($search_query);
if($count == 0){
echo "<h3> NO RESULT</h3>";
}else{
$query = "SELECT * FROM jobs";
$job_display = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($job_display)){
$job_title = $row['job_title'];
$employer = $row['employer'];
$job_date = $row['job_date'];
$job_logo = $row['job_logo'];
$job_desc = $row['job_desc'];
?>
<div class="row">
<div>
<div class="media img-responsive">
<div class="media-left media-middle">
<a href="#">
<img class="media-object" src="images/<?php echo $job_logo; ?>" class="img-responsive" alt="Absa Insurance Logo">
</a>
</div>
<div class="media-body">
<h4 class="media-heading"><span class="job-tittle"><?php echo "{$job_title}";?> </span>(<i class="glyphicon glyphicon-map-marker"> </i>Gauteng, <span class="type blue"> Short-Term Insurance</span>)</h4>
<P>
<?php echo $job_desc;?>
... <i class="glyphicon glyphicon-plus"> </i> Read More</P>
</div>
<div class=" media-right media-middle job-location">
<p> <?php echo $job_date;?> </p>
</div>
</div>
</div>
</div>
<?php }
}
}
?>
here is the Form
<form class=" form-inline" action="search.php" method="post">
<div class="form-group">
<input type="text" name="keyword" class="form-control" placeholder="Job Key Word">
</div>
</form>
Please let me know if you need more information.
you need to remove this line or just filter here
$query = "SELECT * FROM jobs";
In first query You are looking for only selected records
$query = "SELECT * FROM jobs WHERE job_tags LIKE '%$search%'";
$search_query = mysqli_query($connection, $query);
If it find something You search one more time with
$query = "SELECT * FROM jobs";
You don't put WHERE in this query.

Categories