How do make my search work from a class... i can display my search results on the major page but i want it to display all results and if it is more than the paginated result, client can easily move to another page.
<?php
// if it's going to need the database, then it's
//probably smart to require it before we start.
#require_once(LIB_PATH.DS.'database.php');
class Product extends DatabaseObject {
protected static $table_name="products";
protected static $db_fields=array('id', 'product_id', 'image', 'title', 'slug', 'description', 'price');
public $id;
public $product_id;
public $image;
public $title;
public $slug;
public $description;
public $price;
public static function search(){
global $database;
$sql = "SELECT * FROM products WHERE image LIKE '{%$search%'";
$sql .= " OR title LIKE '{%$search%}'";
$sql .= " OR slug LIKE '%$search%'";
$sql .= " OR description LIKE '%$search%'";
$sql .= " OR price LIKE '%$search%'";
$total_count = count($sql);
$result_set = $database->query($total_count);
$row = $database->fetch_assoc($result_set);
return array_shift($row);
}
?>
Please check my code... i am a bit confused.
<?php if(empty($_POST['search'])){
$session->message("<div class='error-msg'>Search cannot be empty</div>");
redirect_to('photos.php');
}
?>
<?php include_layout_template('header2.php'); ?>
<div class="container">
<div class="row">
<?php
if(isset($_POST['submit'])){
// 1. the current page number ($current_page)
$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
// 2. records per page ($per_page)
$per_page = 10;
// 3. total record count ($total_count)
//$total_count = Product::count_all();
$total_count = Product::search();
// Find all photos
// use pagination instead
$pagination = new Pagination($page, $per_page, $total_count);
// Instead of finding all records, just find the records
$search = $database->escape_value($_POST['search']);
// $sql = "SELECT * FROM products WHERE image LIKE '%$search%'";
// $sql .= " OR title LIKE '%$search%'";
// $sql .= " OR slug LIKE '%$search%'";
// $sql .= " OR description LIKE '%$search%'";
// $sql .= " OR price LIKE '%$search%'";
// $sql .= " LIMIT {$per_page} ";
// $sql .= "OFFSET {$pagination->offset()}";
$search = new Product();
$search->search = $search;
$photos = Product::find_by_sql($search);
//$total_count = count($photos);
//echo $numresults = '<p class="error-msg">There are '.$total_count.' results in your search</p><br/><br/>';
foreach ($photos as $photo): ?>
<div class="col-md-4 col-sm-6">
<div class="row">
<div id="pagination" style="clear: both;">
<nav aria-label="Page navigation example">
<ul class="pagination">
<?php
for($i=1; $i <= $pagination->total_pages(); $i++){
if($i == $page) {
// echo " <span class=\"selected\">{$i}</span> ";
// } else{
// echo " {$i} ";
}
}
if($pagination->total_pages() > 1) {
if($pagination->has_previous_page()) {
echo "<li class='page-item'><a class='page-link' href=\"search.php?page=";
echo $pagination->previous_page();
echo "\">« Previous</a></li> ";
}
if($pagination->has_next_page()){
echo "<li class='page-item'><a class='page-link' href=\"search.php?page=";
echo $pagination->next_page();
echo "\">Next »</a></li>";
}
}
?>
</ul>
</nav>
</div>
</div>
<div class="thumbnail">
<form method="post" action="cart.php?action=add&id=<?php echo $photo->id; ?>" role="form" class="form-vertical">
<a href="order_review.php?id=<?php echo $photo->id; ?>"><img src="<?php echo $photo->image_path();
?>" class="img-thumbnail" alt="responsive image"></a>
<div class="caption">
<h3 class="text-info text-center"><?php echo $photo->title; ?></h3>
<p class="text-muted text-center price card-header"><span class="currency">N</span><?php echo $photo->price; ?></p>
<p class="text-center"><em><?php echo $photo->description; ?></em></p>
<input type="hidden" name="id" class="form-control" value="<?php echo $photo->id; ?>" />
<input type="hidden" name="title" class="form-control" value="<?php echo $photo->title; ?>" />
<input type="hidden" name="slug" class="form-control" value="<?php echo $photo->slug; ?>" />
<input type="hidden" name="description" class="form-control" value="<?php echo $photo->description; ?>" />
<input type="hidden" name="price" class="form-control" value="<?php echo $photo->price; ?>"/>
<div class="col-sm-4 mx-auto d-block">
<select name="quantity" class="form-control" name="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
</div>
<div class="col-sm-4 col-sm-push-3 mx-auto d-block">
<input type="submit" name="add_to_cart" class="btn bg-success" value="Add to cart" />
</div><br/>
</form>
</div>
</div>
<?php endforeach; }
?>
</div>
</div>
PLease take note of the code i commented out... I am a bit confused.
Related
I am trying to display the data of a table containing an image in blob format, but I can only get the last inserted record to be displayed.
I would like to show all the records with their respective images, and when adding a new record it will be shown with the rest of the table data.
MYSQL
noticias: idNoticia(PK), idUser(FK), titulo, imagen(LONGBLOB), texto, fecha
PHP - insertarNoticias.php
include('./DB.php');
session_start();
$idUser = $_SESSION['usuario'][0];
$titulo = $_POST['titulo-noticia'];
$texto = $_POST['texto-noticia'];
$fecha = $_POST['fecha-noticia'];
$nombreImagen = $_FILES['imagen']['name'];
$carpetaDestino = $_SERVER['DOCUMENT_ROOT'] . '/masterD/trabajo_final_php/img/';
move_uploaded_file($_FILES['imagen']['tmp_name'], $carpetaDestino . $nombreImagen);
$conexion = DB::conn();
$imagen = fopen($carpetaDestino . $nombreImagen, "r");
$archivoBytes = fread($imagen, intval(filesize($carpetaDestino . $nombreImagen)));
fclose($imagen);
$sentencia = 'INSERT INTO noticias (idUser, titulo, imagen, texto, fecha) VALUES (:idUser, :titulo, :imagen, :texto, :fecha)';
$consulta = $conexion->prepare($sentencia);
$consulta->bindParam(':idUser', $idUser);
$consulta->bindParam(':titulo', $titulo);
$consulta->bindParam(':imagen', $archivoBytes);
$consulta->bindParam(':texto', $texto);
$consulta->bindParam(':fecha', $fecha);
$consulta->execute();
$consulta->closeCursor();
$conexion = null;
PHP - mostrarNoticias.php
include('./DB.php');
session_start();
$titulo = '';
$imagen = '';
$texto = '';
$fecha = '';
$conexion = DB::conn();
$sentencia = 'SELECT * FROM noticias';
$consulta = $conexion->prepare($sentencia);
$consulta->execute();
while ($row = $consulta->fetch(PDO::FETCH_ASSOC)) {
$titulo = $row['titulo'];
$imagen = $row['imagen'];
$texto = $row['texto'];
$fecha = $row['fecha'];
}
$consulta->closeCursor();
$conexion = null;
PHP - adminNoticias.php
<?php
include('./mostrarNoticias.php');
?>
<form class="form" action="./insertarNoticias.php" method="post" id="crear-noticia" enctype="multipart/form-data">
<div class="form-label">
<input type="file" name="imagen" class="form-control mb-3" id="imagen-noticia" required>
<input type="text" class="form-control mb-3" name="titulo-noticia" id="titulo-noticia" placeholder="Título noticia" required>
<input type="text" class="form-control mb-3" name="texto-noticia" id="texto-noticia" placeholder="Texto noticia" required>
<input type="date" class="form-control mb-3" name="fecha-noticia" id="fecha-noticia" required>
</div>
<button type="submit" class="btn btn-success col-12 text-center">
Crear noticia
</button>
</form>
<div class="grid-container pt-4" id="ver-noticias">
<div class="grid-item">
<?php
echo "<h4>$titulo</h4>";
echo "<p>$texto</p>";
echo "<p>$fecha</p>";
echo "<img width='300' height='160' src='data:image/png; base64," . base64_encode($imagen) . "'>";
?>
</div>
</div>
#LuqMan this is what I mean
now it is shown like this
<div class="grid-container">
<div class="grid-item">
<h4></h4>
<p></p>
<p></p>
<img>
<h4></h4>
<p></p>
<p></p>
<img>
</div>
</div>
And I would like it to look like this:
<div class="grid-container">
<div class="grid-item">
<h4></h4>
<p></p>
<p></p>
<img>
</div>
<div class="grid-item">
<h4></h4>
<p></p>
<p></p>
<img>
</div>
</div>
You are fetching the data in a loop, over-writing the same variable. You need to display the variables in the loop. Its best to create a function to display each row and call it from the loop.
One solution is as follows -
Shift the include down to where the display is
PHP - adminNoticias.php
<form class="form" action="./insertarNoticias.php" method="post" id="crear-noticia" enctype="multipart/form-data">
<div class="form-label">
<input type="file" name="imagen" class="form-control mb-3" id="imagen-noticia" required>
<input type="text" class="form-control mb-3" name="titulo-noticia" id="titulo-noticia" placeholder="Título noticia" required>
<input type="text" class="form-control mb-3" name="texto-noticia" id="texto-noticia" placeholder="Texto noticia" required>
<input type="date" class="form-control mb-3" name="fecha-noticia" id="fecha-noticia" required>
</div>
<button type="submit" class="btn btn-success col-12 text-center">
Crear noticia
</button>
</form>
<div class="grid-container pt-4" id="ver-noticias">
<?php
include('./mostrarNoticias.php');
?>
</div>
And show each row here
PHP - mostrarNoticias.php
include('./DB.php');
session_start();
$titulo = '';
$imagen = '';
$texto = '';
$fecha = '';
$conexion = DB::conn();
$sentencia = 'SELECT * FROM noticias';
$consulta = $conexion->prepare($sentencia);
$consulta->execute();
while ($row = $consulta->fetch(PDO::FETCH_ASSOC)) {
$titulo = $row['titulo'];
$imagen = $row['imagen'];
$texto = $row['texto'];
$fecha = $row['fecha'];
?>
<div class="grid-item">
<?php
echo "<h4>$titulo</h4>";
echo "<p>$texto</p>";
echo "<p>$fecha</p>";
echo "<img width='300' height='160' src='data:image/png; base64," . base64_encode($imagen) . "'>";
?>
</div>
<?php
}
$consulta->closeCursor();
$conexion = null;
.......
The very easy solution is shared here, but there may be alternative of this too. The problem in your code is that, your while loop continues its loops and the variables in the loop are replacing its values at each iteration. You can convert each variable to array and then use them in your code where you want to display the table using foreach loop. But the easiest solution is as below:
PHP - mostrarNoticias.php
$output='';
while ($row = $consulta->fetch(PDO::FETCH_ASSOC)) {
$titulo = $row['titulo'];
$imagen = $row['imagen'];
$texto = $row['texto'];
$fecha = $row['fecha'];
$output .= "<h4>$titulo</h4>
<p>$texto</p>
<p>$fecha</p>
<img width='300' height='160' src='data:image/png; base64," . base64_encode($imagen) . "'>";
}
PHP - adminNoticias.php
In your adminNoticias file, replace the following div as below to use the output fetched from previous while loop containing all records.
<div class="grid-container pt-4" id="ver-noticias">
<div class="grid-item">
<?php
echo $output;
?>
</div>
</div>
If you need any explanation, I will appreciate :-)
EDITED VERSION AS PER YOUR COMMENT
The DIV element can be embedded in while loop as below:
PHP - mostrarNoticias.php
$output='';
while ($row = $consulta->fetch(PDO::FETCH_ASSOC)) {
$titulo = $row['titulo'];
$imagen = $row['imagen'];
$texto = $row['texto'];
$fecha = $row['fecha'];
$output .= "<div class='grid-container pt-4' id='ver-noticias'>";
$output .= "<div class='grid-item'><h4>$titulo</h4>";
$output .="<p>$texto</p>
<p>$fecha</p>
<img width='300' height='160' src='data:image/png; base64," . base64_encode($imagen) . "'></div></div>";
}
Now Print the Output in adminNoticias.php file
<?php
echo $output;
?>
This is my whole code....
I am facing the problem while replacing the %20 with - in url. I have tried many codes but failed. please help me out. I am attaching my full code. I am getting problem in
/">Visit
where i am displaying both id as well as firm name. I just want to show both id as well as firm name and they are separated by /
<?php
error_reporting("0");
$rowperpage =10 ;
#$select_city=$_GET['city'];
if(isset($_REQUEST['search'])){
$allcount_query = "SELECT count(*) as allcount FROM inventory_details";
$allcount_result = mysqli_query($conn, $allcount_query);
$allcount_fetch = mysqli_fetch_array($allcount_result);
$allcount = $allcount_fetch['allcount'];
$sql="SELECT * FROM inventory_details";
$search_item = mysqli_real_escape_string($conn, $_REQUEST['search_box']);
$sql .= " WHERE (firm_name LIKE '%$search_item%'";
$sql .= " OR catagory_name LIKE'%$search_item%'";
$sql .= " OR mobile_no LIKE'%$search_item%'";
$sql .= " OR product_key LIKE'%$search_item%')";
$sql .= " AND city='$select_city' AND status='0' ";
$sql .="ORDER BY today_date DESC limit 0,$rowperpage";
// $sql .= " ORDER BY catagory_name LIMIT 0,5";
// echo $sql;
if($_GET['search_box']==""){
$msg=include"error_msg.php";
}
else{
$fquery=mysqli_query($conn, $sql);
$totalrec=mysqli_num_rows($fquery);
if ($totalrec=='0'){
$msg=include"error_msg.php";
}
}
}
// if($selct_city=$_GET['city']){
// SELECT * FROM inventory_details WHERE city LIKE '%DELHI%' && catagory_name LIKE '%belts%'
// SELECT * FROM inventory_details WHERE firm_name='keyboard' OR catagory_name='keyboard' OR mobile_no='keyboard' OR city='keyboard' OR product_key='keyboard' && city="delhi"
// echo $sql . $fquery;
// SELECT * FROM inventory_details WHERE city='delhi' AND catagory_name like '%foot%' OR firm_name LIKE'%foot%' OR product_key like '%foot%' OR mobile_no like '%foot%'
// SELECT * FROM inventory_details WHERE city='Delhi' AND firm_name LIKE '%foot%' OR catagory_name LIKE'%foot%' OR mobile_no LIKE'%foot%' OR product_key LIKE'%foot%'
// // }
?>
<?php
?>
<!-- Brand and toggle get grouped for better mobile display -->
<!-- <div class="container">
<form class="navbar-form" role="search">
<div class="input-group col-md-12 col-xs-12 ">
<div class=" col-md-offset-7 col-md-5">
<select class="form-control select country" name="city" required="">please select city
<?php ;?>
</select>
<select class="form-control input-lg" placeholder="Search By Firm name,Category name,Mobile no....." name="search_box">
<option>select city</option>
<option>Delhi</option>
<option>Mumbai</option>
<option>Channai</option>
<option>Kolkata</option>
</select>
</div>
<div class="input-group-btn ">
</div>
<div class=" col-md-12">
<input type="text" class="form-control" placeholder="Just Type Anything..." name="search_box">
</div>
<div class="input-group-btn ">
<button class="btn btn-info" type="submit" name="search"><span class=""></span>search</button>
</div>
</div>
</form>
</div> -->
<?php
while($row=mysqli_fetch_assoc(#$fquery)){ //handle rows.
$id=$row['id'];
$catagory_name=$row['catagory_name'];
$firm_name=$row['firm_name'];
// $user_pass=md5($_POST ['user_pass']);
$city=$row ['city'];
$product_key=$row ['product_key'];
$firm_email=$row ['firm_email'];
$mobile_no=$row ['mobile_no'];
$phone_no=$row ['phone_no'];
$address=$row ['address'];
$Fax_no=$row ['Fax_no'];
$Website=$row ['Website'];
$product_key=$row['product_key'];
$sotime=$row ['sotime'];
$sctime=$row ['sctime'];
$Contact_person=$row ['Contact_person'];
$Contact_person_mobile=$row ['Contact_person_mobile'];
$colorname=$row ['colorname'];
$textcolor=$row ['textcolor'];
$compLogo=$row ['compLogo'];
////////////////////////////////////////
$banner=$row ['banner'];
if(empty($banner))
{
$banner="default.jpg";
}
?>
<div class="container post" id="post_<?php echo $id; ?>">
<div class="col-md-12 inv_data" style="background:<?php echo $colorname; ?>; color:<?php echo $textcolor; ?>;">
<div class="col-md-3">
<!-- <?php ?>
<img src="image/banner/<?php echo $banner; ?>" class="img_style"> -->
<?php
$imageArr = explode(',',$compLogo);
foreach ($imageArr as $k=>$val) { if($val!=""){
?>
<img class="mySlides" src="image/logo/<?php echo $val; ?>">
<?php
}}
?>
<img class="mySlides" src="image/logo/defualt.jpg">
</div>
<div class="col-md-7">
<h3><strong><?php echo strtoupper($firm_name); ?></strong></h3>
<strong></strong> <?php echo $product_key; ?><br>
<strong> <span class="glyphicon glyphicon-home"></span> </strong> <?php echo $address; ?><br>
<!-- <strong>Category : -</strong> <?php echo $catagory_name." , " . $product_key;?><br> -->
<!-- <strong>Phone no : -</strong> <?php echo $phone_no; ?>
<strong>Mobile no : -</strong> <?php echo $mobile_no; ?>
<br>
<strong> Whatsapp no : -</strong> <?php echo $Contact_person_mobile ; ?>
<strong> Contact Person : -</strong> <?php echo $Contact_person ; ?>
<br>
<strong>Email : -</strong> <?php echo $firm_email;?>
<strong>Timing : -</strong> <?php echo $sotime. " to ".$sctime?><br>
<strong>Website : -</strong> <?php echo $Website;?><br> -->
</div>
<div class="col-md-2">
<span> <br> <br> <br></span>
<button type="button" class="btn btn-info btn-lg">Visit</button>
<!--<a href="/jyp/<?php echo $id;?><?php echo $firm_name;?>"><button type="button" class="btn btn-info btn-lg">Visit</button>-->
</a>
</div>
</div>
</div>
<?php
}
?>
<?php
if(isset($_REQUEST['search'])){
?>
<h1 id="loadbtn"><span class="load-more">Load More</span></h1>
<input type="hidden" id="row" value="0">
<input type="hidden" id="cityname" name="cityname" value="<?php echo $city; ?>">
<input type="hidden" id="search_item" name="search_item" value="<?php echo $search_item; ?>">
<input type="hidden" id="all" value="<?php echo $allcount; ?>">
<?php
}
?>
The %20 gives a hint that the string is most probably Url Encoded (%20 is a single space character). So what you should probably be doing is trying to first Url Decode the string (then you will get the original string). Then afterwards you can remove/replace any characters that you do not want.
So in your question, you said you do not want %20 (a space) but a dash (-) instead. This could be achieved as follows...
<?php
error_reporting("0");
$rowperpage =10 ;
#$select_city=$_GET['city'];
if(isset($_REQUEST['search'])){
$allcount_query = "SELECT count(*) as allcount FROM inventory_details";
$allcount_result = mysqli_query($conn, $allcount_query);
$allcount_fetch = mysqli_fetch_array($allcount_result);
$allcount = $allcount_fetch['allcount'];
$sql="SELECT * FROM inventory_details";
$search_item = mysqli_real_escape_string($conn, $_REQUEST['search_box']);
$sql .= " WHERE (firm_name LIKE '%$search_item%'";
$sql .= " OR catagory_name LIKE'%$search_item%'";
$sql .= " OR mobile_no LIKE'%$search_item%'";
$sql .= " OR product_key LIKE'%$search_item%')";
$sql .= " AND city='$select_city' AND status='0' ";
$sql .="ORDER BY today_date DESC limit 0,$rowperpage";
// $sql .= " ORDER BY catagory_name LIMIT 0,5";
// echo $sql;
if($_GET['search_box']==""){
$msg=include"error_msg.php";
}
else{
$fquery=mysqli_query($conn, $sql);
$totalrec=mysqli_num_rows($fquery);
if ($totalrec=='0'){
$msg=include"error_msg.php";
}
}
}
// if($selct_city=$_GET['city']){
// SELECT * FROM inventory_details WHERE city LIKE '%DELHI%' && catagory_name LIKE '%belts%'
// SELECT * FROM inventory_details WHERE firm_name='keyboard' OR catagory_name='keyboard' OR mobile_no='keyboard' OR city='keyboard' OR product_key='keyboard' && city="delhi"
// echo $sql . $fquery;
// SELECT * FROM inventory_details WHERE city='delhi' AND catagory_name like '%foot%' OR firm_name LIKE'%foot%' OR product_key like '%foot%' OR mobile_no like '%foot%'
// SELECT * FROM inventory_details WHERE city='Delhi' AND firm_name LIKE '%foot%' OR catagory_name LIKE'%foot%' OR mobile_no LIKE'%foot%' OR product_key LIKE'%foot%'
// // }
?>
<?php
?>
<!-- Brand and toggle get grouped for better mobile display -->
<!-- <div class="container">
<form class="navbar-form" role="search">
<div class="input-group col-md-12 col-xs-12 ">
<div class=" col-md-offset-7 col-md-5">
<select class="form-control select country" name="city" required="">please select city
<?php ;?>
</select>
<select class="form-control input-lg" placeholder="Search By Firm name,Category name,Mobile no....." name="search_box">
<option>select city</option>
<option>Delhi</option>
<option>Mumbai</option>
<option>Channai</option>
<option>Kolkata</option>
</select>
</div>
<div class="input-group-btn ">
</div>
<div class=" col-md-12">
<input type="text" class="form-control" placeholder="Just Type Anything..." name="search_box">
</div>
<div class="input-group-btn ">
<button class="btn btn-info" type="submit" name="search"><span class=""></span>search</button>
</div>
</div>
</form>
</div> -->
<?php
while($row=mysqli_fetch_assoc(#$fquery)){ //handle rows.
$id=$row['id'];
$catagory_name=$row['catagory_name'];
$firm_name=$row['firm_name'];
// $user_pass=md5($_POST ['user_pass']);
$city=$row ['city'];
$product_key=$row ['product_key'];
$firm_email=$row ['firm_email'];
$mobile_no=$row ['mobile_no'];
$phone_no=$row ['phone_no'];
$address=$row ['address'];
$Fax_no=$row ['Fax_no'];
$Website=$row ['Website'];
$product_key=$row['product_key'];
$sotime=$row ['sotime'];
$sctime=$row ['sctime'];
$Contact_person=$row ['Contact_person'];
$Contact_person_mobile=$row ['Contact_person_mobile'];
$colorname=$row ['colorname'];
$textcolor=$row ['textcolor'];
$compLogo=$row ['compLogo'];
////////////////////////////////////////
$banner=$row ['banner'];
if(empty($banner))
{
$banner="default.jpg";
}
?>
<div class="container post" id="post_<?php echo $id; ?>">
<div class="col-md-12 inv_data" style="background:<?php echo $colorname; ?>; color:<?php echo $textcolor; ?>;">
<div class="col-md-3">
<!-- <?php ?>
<img src="image/banner/<?php echo $banner; ?>" class="img_style"> -->
<?php
$imageArr = explode(',',$compLogo);
foreach ($imageArr as $k=>$val) { if($val!=""){
?>
<img class="mySlides" src="image/logo/<?php echo $val; ?>">
<?php
}}
?>
<img class="mySlides" src="image/logo/defualt.jpg">
</div>
<div class="col-md-7">
<h3><strong><?php echo strtoupper($firm_name); ?></strong></h3>
<strong></strong> <?php echo $product_key; ?><br>
<strong> <span class="glyphicon glyphicon-home"></span> </strong> <?php echo $address; ?><br>
<!-- <strong>Category : -</strong> <?php echo $catagory_name." , " . $product_key;?><br> -->
<!-- <strong>Phone no : -</strong> <?php echo $phone_no; ?>
<strong>Mobile no : -</strong> <?php echo $mobile_no; ?>
<br>
<strong> Whatsapp no : -</strong> <?php echo $Contact_person_mobile ; ?>
<strong> Contact Person : -</strong> <?php echo $Contact_person ; ?>
<br>
<strong>Email : -</strong> <?php echo $firm_email;?>
<strong>Timing : -</strong> <?php echo $sotime. " to ".$sctime?><br>
<strong>Website : -</strong> <?php echo $Website;?><br> -->
</div>
<div class="col-md-2">
<span> <br> <br> <br></span>
<!-- there's the solution below -->
<?php
$firm_name = urldecode($firm_name); # decode the string (like converting %20 to a space character)
$firm_name = str_replace(' ', '-', $firm_name); # then replace any characters you don't want (like converting a space character to a -
$url = "category.php?edit=" . urlencode($id) . '/' . urlencode($firm_name);
?>
<button type="button" class="btn btn-info btn-lg">Visit</button>
<?php
// probably don't need these now.
$firm_name = urldecode($firm_name);
$firm_name = str_replace(' ', '-', $firm_name);
# are you missing a / here ???
$url = '/jyp/' . urlencode($id) . '' . urlencode($firm_name);
?>
<a href="<?php echo $url ?>"><button type="button" class="btn btn-info btn-lg">Visit</button>
</a>
</div>
</div>
</div>
<?php
}
?>
<?php
if(isset($_REQUEST['search'])){
?>
<h1 id="loadbtn"><span class="load-more">Load More</span></h1>
<input type="hidden" id="row" value="0">
<input type="hidden" id="cityname" name="cityname" value="<?php echo $city; ?>">
<input type="hidden" id="search_item" name="search_item" value="<?php echo $search_item; ?>">
<input type="hidden" id="all" value="<?php echo $allcount; ?>">
<?php
}
?>
You probably should encode whatever you echo to the browser using htmlspecialchars.
Disclaimer: The website where I was testing my PHP code gave the same result when using rawurldecode('hi%20you'); and urldecode('hi%20you');.
I am going to build a dynamic product category with their subs for my final year project. I tried the tree way but somehow it make me confused so i decided to make it as simple. I want to display Clothe's parent name.I want to display it like this
Can i do it so ?
Here is my PHP code
<form method="post" action="product_category_add_exec.php" enctype="multipart/form-data">
<div class="form-group">
<label for="recipient-level" class="control-label"> Parent Category</label>
<select class="form-control" name="admin_lid" required="">
<option></option>
<?php
$sql_pcat = "SELECT * FROM product_category";
$select_pcat = mysqli_query($db,$sql_pcat) or die (mysqli_error().$sql_pcat);
$x =1;
while($list_pcat = mysqli_fetch_array($select_pcat))
{
$product_cat_id = $list_pcat['product_cat_id'];
$parent_id = $list_pcat['parent_id'];
$product_cat_name = $list_pcat['product_cat_name'];
?>
<?php
if ($parent_id == 0)
{
?>
<option value = "<?php echo $product_cat_id;?>"><?php echo $product_cat_name; ?></option>
<?php
} else
{
$sql_cat = "SELECT * FROM product_category WHERE parent_id= $parent_id ORDER BY product_cat_name ASC";
$select_cat = mysqli_query($db,$sql_cat) or die (mysqli_error().$sql_cat);
$list_cat = mysqli_fetch_array($select_cat);
$product_cat_id = $list_cat['product_cat_id'];
$parent_id = $list_cat['parent_id'];
$product_cat_name = $list_cat['product_cat_name'];
?>
<option value = "<?php echo $parent_id;?>">--<?php echo $parent_id;?><?php echo $product_cat_name; ?></option>
<?php
}
?>
<?php
$x++;
}
?>
</select>
</div>
<div class="form-group">
<label for="recipient-category" class="control-label">Product Name </label>
<input type="text" class="form-control" id="recipient-category" name="product_cat_name">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-info">Add</button>
<button type ="reset" class ="btn btn-danger">Reset</button>
</div>
</form>
This is my database table
I want to make it appear like this
I have this code:
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
while($langs = mysqli_fetch_assoc($r)){
$l_id = $langs['id'];
$l_name = $langs['name_en'];
?>
<li <?php if($l_id == '1'){ echo 'class="active"'; }?>><?php echo $l_name;?></li>
<?php } // closing 1st while loop
?>
</ul>
<div class="tab-content">
<?php
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
while($langs = mysqli_fetch_assoc($r)){
$l_id = $langs['id'];
$l_lang = $langs['language'];
$l_name = $langs['name_en'];
?>
<div class="tab-pane fade <?php if($l_id == '1'){ echo 'in active'; }?>" id="<?php echo $l_name;?>">
<div class="form-group col-xs-4">
<label for="title_<?php echo $l_lang;?>">Title:</label>
<input class="form-control" type="text" name="title_<?php echo $l_lang;?>" id="title_<?php echo $l_lang;?>" value="<?php echo $opened['title_'.$l_lang.'']; ?>" placeholder="Page Title">
</div>
<div class="form-group col-xs-4">
<label for="header_<?php echo $l_lang;?>">Header:</label>
<input class="form-control" type="text" name="header_<?php echo $l_lang;?>" id="header_<?php echo $l_lang;?>" value="<?php echo $opened['header_'.$l_lang.'']; ?>" placeholder="Page Header">
</div>
<div class="form-group col-xs-<?php if($page_type == 'tour'){ echo 8;}else {echo 12;} ?>">
<label for="body_<?php echo $l_lang;?>">Body:</label>
<textarea class="form-control editor" name="body_<?php echo $l_lang;?>" id="body_<?php echo $l_lang;?>" rows="7" placeholder="Page Body"><?php echo $opened['body_'.$l_lang.'']; ?></textarea>
</div>
</div>
<?php } //closing 2nd while loop
?>
</div>
When running it, the result is a tabbed form (I skipped the form tags and some html from above, to reduce the code writen) and everything is OK.
My questions are:
How to have the same output, but with a single query and while loop?
Is it possible to make this a function? Any hints?
Thank you!
I think you will have to loop twice, but you don't need to make two queries at all!
Use mysqli_fetch_all to store the results in an array and then loop through it
For example:
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
$langs = mysqli_fetch_all($r);
foreach($langs as $lang){
//render links
}
//...
foreach($langs as $lang){
//render tabs
}
Your script will run much faster
Try
<?php
function get_rows()
{
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
$languages = array();
while($langs = mysqli_fetch_assoc($r))
{
$languages[] = $langs;
}
return $languages;
}
$languages = get_rows();
if($languages != false)
{
?>
<ul>
<?php
foreach($languages as $langs)
{
$l_id = $langs['id'];
$l_name = $langs['name_en'];
?>
<li <?php
if($l_id == '1')
{
echo 'class="active"';
}
?>><?php echo $l_name; ?></li>
<?php
}
?>
</ul>
<div class="tab-content">
<?php
foreach($languages as $langs)
{
$l_id = $langs['id'];
$l_lang = $langs['language'];
$l_name = $langs['name_en'];
?>
<div class="tab-pane fade <?php
if($l_id == '1')
{
echo 'in active';
}
?>" id="<?php echo $l_name; ?>">
<div class="form-group col-xs-4">
<label for="title_<?php echo $l_lang; ?>">Title:</label>
<input class="form-control" type="text" name="title_<?php echo $l_lang; ?>" id="title_<?php echo $l_lang; ?>" value="<?php echo $opened['title_' . $l_lang . '']; ?>" placeholder="Page Title">
</div>
<div class="form-group col-xs-4">
<label for="header_<?php echo $l_lang; ?>">Header:</label>
<input class="form-control" type="text" name="header_<?php echo $l_lang; ?>" id="header_<?php echo $l_lang; ?>" value="<?php echo $opened['header_' . $l_lang . '']; ?>" placeholder="Page Header">
</div>
<div class="form-group col-xs-<?php
if($page_type == 'tour')
{
echo 8;
}
else
{
echo 12;
}
?>">
<label for="body_<?php echo $l_lang; ?>">Body:</label>
<textarea class="form-control editor" name="body_<?php echo $l_lang; ?>" id="body_<?php echo $l_lang; ?>" rows="7" placeholder="Page Body"><?php echo $opened['body_' . $l_lang . '']; ?></textarea>
</div>
</div>
<?php } ?>
</div>
<?php
}
?>
Save results while first iterating in some array and the iterate over this array for the second time:
$tmp_array = array();
while ($langs = mysqli_fetch_assoc($r)){
$tmp_array[] = $langs;
}
//some code here
//and second array
foreach ($tmp_array as $langs) {
//more code here
}
I have problem, I am trying to create a form with a few checkboxes, each assigned a different value, i just can update value first and last, between not working, can you help me?
Check.php
<div class="top-on">
<div class="top-on1">
<p class="text-center"> <?php echo $row['username'];?></p>
<br>
<select class="form-control col-sm-12" name="edit_level">
<?php
global $pdo;
$sql = $pdo->query("SELECT * FROM level");
while ( $row_c = $sql->fetch(PDO::FETCH_ASSOC) ) {
?>
<option <?php if($row_c["level"]==$row["level"])
{
echo "selected=\"selected\"";
}?>
value="<?php echo $row_c['level']?>"> <?php echo $row_c['name'];?> </opition>
<?php } ?>
</select>
</div>
<label style="float: right;">
<input type="checkbox" class="checkbox" name="idlevel[]" value="<?php echo $row['id'];?>"> </label>
<div class="clearfix"> </div>
</div>
page Control.php
function edit_level(){
global $pdo;
$sql1="SELECT * From user ";
$stmt1 = $pdo->query($sql1);
if(isset($this->btnlevel))
{
for($i=0;$i<$stmt1->rowCount();$i++){
$elve=$this->idlevel[$i];
$sql ="UPDATE user SET level='$this->editlevel' WHERE id='".$elve."'";
$upt = $pdo->prepare($sql);
$upt->execute();
}
}
}