I need help displaying data in php - php

I am just learning php and I have a problem with a project. I have a database in mysql (localhost) and in it I have 3 tables: categories, products and restaurants, categories and restaurants are linked to products. And when I try to display products of a specified category it doesn't display anything. The connection to the database is good and the queries are good so I have no idea what's wrong. Pleas help.
This is the index.php
<?php
include_once './database.php';
include_once './header.php';
<div class="content" align="center">
<div id="galery" class="content_block" style="height:400px;">
<div class="galery">
<img src="./img/gallery/6.jpg">
</div>
</div>
<div class="content_block" style="padding-bottom:10px;">
$query = "SELECT * FROM categories";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)){
echo '<div class="contentButton inline"><div class="catFood_img foodCategory_1"></div><div>'.$row['category'].'</div></div>';
</div>
<div class="content_block" style="height:190px" align="center">
$query = "SELECT * FROM restourants";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)){
echo '<div class="restournats inline"><img src="./food/restourants/chens.jpg"></div>';
}
</div>
</div>
include_once './footer.php';
?>
And this is the code for categories.php
include_once './database.php';
include_once './header.php';
<div class="content" align="center">
<div class="content_block2">
$category= (int)(isset($_GET['id_category']) ? $_GET['id_category'] : '');
$query = "SELECT * FROM products WHERE products.id_category=$category";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)){
echo '
<a href="dishes.php?id='.$row['id'].'"><div class="item inline">
<div class="food_img"><img src="'.$row['img'].'" /></div>
<div class="title">'.$row['name'].'</div>
<div class="price inline">Price: '.$row['price'].' €</div><div class="btn">Choose</div>
</div>
</a>';
}
</div>
</div>
include_once './footer.php';

In your index.php you are creating the link as
echo '<a href="categories.php?cat='.$row['category'].'">
(double check $row['category'] given above is the desired category id value you need)
But in your categories.php you are trying to get the category as
$category= (int)(isset($_GET['id_category']) ? $_GET['id_category'] : '');
Try $_GET['cat'] instead of $_GET['id_category']
Also try echo $category and echo $query to see if you are getting the desired values.

Related

How do I filter with dropdown list? PHP and SQL

We are trying to filter based on the tags or the dropdown menu, we are trying to create a blog website that has tags and those tags can be used to filter the posted content on the homepage Tags dropdown menu:. Here should be the content in the page: Here should be the content in the page:
<nav>
<ul>
<li>Home</li>
<li>Tags<i class="material-icons">arrow_drop_down</i>
<ul class = "dropdown">
<li>All</button></li>
<li>Homemade</button></li>
<li>Pro</button></li>
<li>Resto</button></li>
</ul>
</li>
<!-- Posting -->
<div>
<?php
$query = "select * from posts order by date limit 8";
$result = mysqli_query($con,$query,$tagHomemade);
?>
<?php if(mysqli_num_rows($result) > 0):?>
<div>
<?php while ($row = mysqli_fetch_assoc($result)):?>
<?php
$user_id = $row['user_id'];
$query = "select username, image from users where id ='$user_id' limit 1";
$res = mysqli_query($con,$query);
$user_row = mysqli_fetch_assoc($res);
?>
<div class="card">
<div style ="display: flex;">
<div style ="flex:1", >
<div class="profile"><img class ="icon" src="<?=$user_row['image']?>"></div>
</div>
<div style ="flex:2;" >
<h5>Posted by: <?php echo $_SESSION['logged']['username']?>,
<?php echo date("jS M, Y",strtotime($row['date']))?>
</h5>
<h3><?php echo $row['tag']?>: <?php echo $row['title']?></h>
</div>
</div>
<div>
<?php if (file_exists($row['image']))?>
<div class="img" style="text-align:center;">
<img class="postPic" src="<?=$row['image']?>">
</div>
</div>
<div>
<?php if (!empty ($POST['post']));?>
<?php echo $row['post']?>
</div>
</div>
<?php endwhile;?>
</div>
</div>
<?php endif;?>`
</div>
Set tags in the url like this:
<li>Homemade</button></li>
Then you can get it by $_GET['tag'] in your code. Then put it on the SQL query.
Good to know: It's strongly recommended to use prepared statements and don't use $_GET['tag'] directly in your SQL code. It prevents SQL injection and cares about special characters as well.
Update:
<nav>
<ul>
<li>Home</li>
<li>Tags<i class="material-icons">arrow_drop_down</i>
<ul class = "dropdown">
<li>All</button></li>
<li>Homemade</button></li>
<li>Pro</button></li>
<li>Resto</button></li>
</ul>
</li>
<!-- Posting -->
<div>
<?php
$where = !empty($_GET['tag']) ? "where tag = '" . $_GET['tag'] . "'" : "";
$query = "select * from posts $where order by date limit 8";
$result = mysqli_query($con,$query);
?>
<?php if(mysqli_num_rows($result) > 0):?>
<div>
<?php while ($row = mysqli_fetch_assoc($result)):?>
<?php
$user_id = $row['user_id'];
$query = "select username, image from users where id ='$user_id' limit 1";
$res = mysqli_query($con,$query);
$user_row = mysqli_fetch_assoc($res);
?>
<div class="card">
<div style ="display: flex;">
<div style ="flex:1", >
<div class="profile"><img class ="icon" src="<?=$user_row['image']?>"></div>
</div>
<div style ="flex:2;" >
<h5>Posted by: <?php echo $_SESSION['logged']['username']?>,
<?php echo date("jS M, Y",strtotime($row['date']))?>
</h5>
<h3><?php echo $row['tag']?>: <?php echo $row['title']?></h>
</div>
</div>
<div>
<?php if (file_exists($row['image']))?>
<div class="img" style="text-align:center;">
<img class="postPic" src="<?=$row['image']?>">
</div>
</div>
<div>
<?php if (!empty ($POST['post']));?>
<?php echo $row['post']?>
</div>
</div>
<?php endwhile;?>
</div>
</div>
<?php endif;?>
</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.

How can I include an image that is tied to a user's database information?

I've been coding PHP for 2 weeks (it's not pretty) and I have not been able to find the answer to my question. I want an admin type user to be able to fill a form and post it to a page where base level users can view the content. I've gotten all of this to work like a charm, but my dilemma is to allow the admin user to include an image as well. Maybe I just don't know what to search for.
Here is the php code and the form for the admin user page:
<?php
error_reporting(E_ALL & ~E_NOTICE);
session_start();
include_once("connection.php");
if (isset($_SESSION['adminid'])) {
$adminid = $_SESSION['adminid'];
$adminemail = $_SESSION['adminemail'];
if ($_POST['submit']) {
$title = $_POST['title'];
$deadline = $_POST['deadline'];
$content = $_POST['content'];
$sql_blog = "INSERT INTO blog (title, deadline, content, logoname) VALUES ('$title', '$deadline', '$content', '$logoname')";
$query_blog = mysqli_query($dbcon, $sql_blog);
echo "<script>alert('Your inquiry has been posted')</script>";
}
} else {
header('Location: index.php');
die();
}
$sql = "SELECT adminid, adminemail, adminpassword, adminname FROM admin WHERE adminemail = '$adminemail' LIMIT 1";
$query = mysqli_query($dbcon, $sql);
if ($query) {
$row = mysqli_fetch_row($query);
$adminname = $row[3];
}
?>
and here is the code for the base level user page: (i commented out the image block where I want the admin's image to be shown.
<main>
<div class="container">
<div class="row topbuffpost">
<h1>business inquiries</h1>
<hr>
<?php
include_once('connection.php');
$sql = "SELECT * FROM blog ORDER BY id DESC";
$result = mysqli_query($dbcon, $sql);
while ($row = mysqli_fetch_array($result)) {
$title = $row['title'];
$content = $row['content'];
$date = strtotime($row['deadline']);
?>
<div class="col-md-4 col-lg-3">
<div class="card hoverable">
<!-- <div class="card-image">
<div class="view overlay hm-white-slight z-depth-1">
<img src="">
<a href="#">
<div class="mask waves-effect">
</div>
</a>
</div>
</div> -->
<div class="card-content">
<h5> <?php echo $title; ?> <br/> <h6>Deadline |<small> <?php echo date("j M, Y", $date); ?> </small> </h6></h5> <br/>
<p> <?php echo $content; ?> </p>
<div class="card-btn text-center">
Read more
<i class="fa fa-lightbulb-o"></i>&nbsp propose a plan
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</main>
All of this works perfectly, I just can't figure out how to have an image display in the same way as the title, deadline, and content. Youtube wont help either, too much outdated php + I haven't been coding long enough to really work things out on my own.
You can save all user images under a folder (let's call /images/user) and record the file name into database.
if ($_POST['submit']) {
$title = $_POST['title'];
$deadline = $_POST['deadline'];
$content = $_POST['content'];
$logoname = basename($_FILES["fileToUpload"]["logoname"]; // <-- Make sure your form is ready to submit an file
// Update below as per your need.
$target = 'images/users/' . $logoname;
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target);
$sql_blog = "INSERT INTO blog (title, deadline, content, logoname) VALUES ('$title', '$deadline', '$content', '$logoname')";
$query_blog = mysqli_query($dbcon, $sql_blog);
echo "<script>alert('Your inquiry has been posted')</script>";
}
You can then display the image your page
<main>
<div class="container">
<div class="row topbuffpost">
<h1>business inquiries</h1>
<hr>
<?php
include_once('connection.php');
$sql = "SELECT * FROM blog ORDER BY id DESC";
$result = mysqli_query($dbcon, $sql);
while ($row = mysqli_fetch_array($result)) {
$title = $row['title'];
$content = $row['content'];
$date = strtotime($row['deadline']);
$logoname = 'images/user/' . $row['logoname'];
?>
<div class="col-md-4 col-lg-3">
<div class="card hoverable">
<div class="card-image">
<div class="view overlay hm-white-slight z-depth-1">
<img src="<?php echo $logoname; ?>">
<a href="#">
<div class="mask waves-effect">
</div>
</a>
</div>
</div>
<div class="card-content">
<h5> <?php echo $title; ?> <br/> <h6>Deadline |<small> <?php echo date("j M, Y", $date); ?> </small> </h6></h5> <br/>
<p> <?php echo $content; ?> </p>
<div class="card-btn text-center">
Read more
<i class="fa fa-lightbulb-o"></i>&nbsp propose a plan
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</main>

How to make my php slider work

My php slider is sort of working i have managed to get it to link to my database. However, i need it to loop through all the images within my slider and my code isn't work but i believe i have missed a loop query?!
THIS IS MY CODE WHERE MY SLIDER IS:
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
<img src="<?php print $row['image']?>"/>
<!-- <img src="images/nivo/arts.png" alt="the grand theatre and nothern ballet">
<img src="images/nivo/slider3.png" alt="leeds night light slider image">
<img src="images/nivo/slider2.png" alt="Leeds Trinity slider image">
<img src="images/nivo/slider4.png" alt="leeds art hotels">
<img src="images/nivo/slider1.png" alt="leeds art slider image"> -->
</div>
</div>
THIS IS MY CODE TO RUN MY SLIDER AT THE MOMENT:
<?php
$myQuery = "SELECT * FROM SliderImg";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));
$row = mysqli_fetch_array($result);
?>
you need a loop in your php:
<?php
$myQuery = "SELECT * FROM SliderImg";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));
$rows = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$rows[] = array(
'image' => $row['image'];
)
}
?>
And in your HTML:
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
<?php
foreach($rows as $row) { ?>
<img src="<?php print $row['image']?>"/>
<?php
} ?>
</div>
</div>
<?php
$myQuery = "SELECT * FROM SliderImg";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));
?>
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
// use a while here
<?php while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){ ?>
<img src="<?php print $row['image']?>"/>
<?php } ?>
</div>
</div>
you can use foreach to loop through your data, like this
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
<?php foreach ($row as $key => $item): ?>
<img src="<?php echo $item['image']?>"/>
<?php endforeach; ?>
</div>
</div>
hope it help..

Categories