Php search function displaying null - php

Hi guys so i am just using learning php for the first time and building my own site etc to try it out. I have a database of recipes. For each recipe it has a list of ingredients. Each recipe will have different amounts. So one will have 5, the other can have 3 etc. The problem with my code is. If someone searches for a recipe and they find it, it will return the ingredients but sometimes if it has more than the divs i put there it will give some null values back. Also i understand about the sql injections etc and the bad practice but i am just playing about with it first. I want to get it to work and then fix that part later :)
PHP:
<div class="panel panel-default">
<div class="panel-heading"><b>' . htmlentities($rN, ENT_QUOTES) . '</b></div>
</a>
}
?>
Now i am pretty sure in my white loop i am suppose to do an if statement after the div tags and say if the value == null then dont display but i have tried and nothing has worked so any help on this matter would be great
Thanks

Your HTML for ingredients seems be repeating, so you can resolve the empty <div> issue and short your code using a for loop and a if condition:
$output .= '<div class="panel panel-default">
(...)
<h3 class="media-heading">INGREDIENTS:</h3>';
for( $i=1; $i < 7; $i++ )
{
if( ${"rI$i"} )
{
$output .= '<div class="food-graph">
<span class="food-graph-title">' . htmlentities(${"rI$i"}, ENT_QUOTES) . '</span>
</div>';
}
}
$output .= ' (...) ';

If encountering null values is a problem, this will only print out the ingredients that your row contains.
while($row = mysql_fetch_array($query)) {
$rN = $row['recipeName'];
$i=1;
$recipes = '';
if (isset($row['recipe_ing'.$i]) {
while(isset($row['recipe_ing'.$i]) {
$value = htmlentities($row['recipe_ing'.$i], ENT_QUOTES);
$recipes .= <<< EOT
<div class="food-graph">
<span class="food-graph-title">$value</span>
</div>
EOT;
$i++;
}
$output .= <<< EOT
<div class="panel panel-default">
<div class="panel-heading"><b>' . htmlentities($rN, ENT_QUOTES) . '</b></div>
<div class="panel-body">
<div class="pull-left col-xs-12 col-sm-4">
<a href="#">
<img class="img-thumbnail img-responsive" src="Image/green.jpg">
</a>
<a class="btn btn-success btn-block btnrec" href="#">View Recipe</a>
</div>
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="media-body">
<h3 class="media-heading">INGREDIENTS:</h3>
$recipes
</div>
</div>
</div>
</div>
<div class="panel-footer">Rating</div>
</div>
EOT;
}
}
EDIT: Also, i'm using Heredocs to set the strings. the lines with EOT; must have no whitespace or code in front of it. it must be at the start of the line. also no code may be on the same line behind it. Else, your documents will become a huge string.

Related

Footer is being affected by bootstrap c;ass

I am currently working on the store page for my website and it seems that I have come across a problem. I have created a search function for my website. In the search.php page, however, where items are displayed according to the search, a bootstrap class seems to be interfering with my footer. When I remove the class the footer fits fine in the page but the product cards get messed up. When I add the class back in my footer shrinks and distorts. What can I do in order for that not to happen?
Here is my code:
search.php:
while($row = $stmt->fetch()){
$stock = $row['stock'];
if ($stock > 0){
echo '
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3">
<form method="post" action="view_product.php?action=add&id='.$row["ID"].'">
<div class="products">
<a href="view_product.php?product='.$row["ID"].'">
<img src="'.$row["image"].'" class="product-img">
</a>';
echo' <div class="product-txt" style="padding: 5px;">
<h4 style="line-height: 10px;">
<a href="view_product.php?product='.$row['ID'].'">
<b>'.$row['name'].'</b>
</a>
<br>
<h4>';
if($stock > 0){
echo ''.$row["price"].'';}
else{
echo ''.$row['outofstock'].'';
}
echo' </h4>
</h4>
</div>
</div>
</form>
</div>
</body>
';
}
}
include 'footer.php';
The "col-xs-12 col-sm-6 ...." class is interfering with my footer. What can I do in order ti fix this. Thanks in advance!
Here are your bugs:
echo' </h4>
</h4>
</div>
</div>
</form>
</div>
</body>
';
}
}
include 'footer.php';
First - double closing </h4>, you cannot have <h4> inside <h4>, but the real problem is including text of footer after closing </body>.
This happens sometimes, but you'd better use a validator w3c
update
That's how it should go
while($row = $stmt->fetch()){
$stock = $row['stock'];
if ($stock > 0){ echo
"<div class=\"col-xs-12 col-sm-6 col-md-4 col-lg-3\">\r\n
<form method=\"post\" action=\"view_product.php?action=add&id=$row['ID']\">
<div class=\"products\">\r\n
<img src=\"$row['image']\" class=\"product-img\"></div>\r\n
<div class=\"product-txt\" style=\"padding: 5px\">\r\n
<h4 style=\"line-height: 10px\">
<b>$row['name']</b>
$row['price']
</h4></div></form></div>\r\n";}
else{ echo "<p>$row['outofstock']</p>\r\n";}}
include './footer.php';
echo'</body></html>';
The point is, you have to control your loops and HTML tags - what is open must be closed in time. If you get lost, take a look at the source code in your browser. And use the validator w3c like the best designers do.

Make a pagination for cards php

I have these cards with data that comes from a database. This is the reason it is in a while loop.
Now my problem is that I don't have a clue how to add pagination to this. If some of you guys have any suggestions or answers please let me know.
$start_from = ($page - 1)*$record_per_page;
$query = "SELECT organisation_name, organisation_logo, organisation_url FROM `organisations`";
$result = mysqli_query($conn, $query);
if ($result ->num_rows > 0) {
echo '<div id="myItems">';
echo '<div class="row">';
while ($row = $result-> fetch_assoc()) {
echo '
<div class="col-md-6">
<div class="card rounded-lg mt-3">
<div class="card-body p-0 shadow">
<div class="row">
<div class="col-md-4 p-0">
<img src="../img/';
if ($row['organisation_logo'] == '') {
echo 'stock/no-image-icon.png"';
} else {
echo 'uploads/'.$row['organisation_logo'].'"';
}
echo 'class="border p-3" style=" width: 150px; height: 150px;">
</div>
<div class="col-md-8">
<h3 class="card-title mt-3">'.$row["organisation_name"].' <a class="text-dark" href="http://'.$row["organisation_url"].'" target="_blank"><i class="fas fa-external-link-alt" style="font-size: 18px;"></i></a></h3>
<p>Current active surveys: <b>0</b></p>
<b>View <i class="fas fa-arrow-circle-right"></i></b>
</div>
</div>
</div>
</div>
</div>';
}
echo '</div>';
}
else {
echo 'nothing here';
}
You need to use LIMIT and OFFSET functionality in your SQL query. Limit determines how many elements you want to show on one site. Offset is point from where you want to get your items. So, for example – if You want 5 items, and you are on page 3, you have to add LIMIT 5 OFFSET 10. You calculate offset with (page_number-1) * limit equation. If you want to count how many pages you have, you must get total pages number, and use something like $pages = ceil($total / $limit). There are a lot of examples on stackoverflow.
I think this one can be very helpful – Simple PHP Pagination script
You also don't need to do all the calculation on your own, there are plany of pagination library for PHP, check look for them.
Hope it helps you a little.

Javascript not working in PHP foreach?

I'm using this code to get a maximum of text in a p tag. For one reason this code only runs on the first card in my foreach but not on the second.
Here you see the problem: https://gyazo.com/c3ef858fb233b21b31098fb1682a7ce4
Here is my javascript code:
<script>
function truncateText(selector, maxLength) {
var element = document.querySelector(selector),
truncated = element.innerText;
if (truncated.length > maxLength) {
truncated = truncated.substr(0,maxLength) + '...';
}
return truncated;
}
</script>
[PHP CODE]
<?php
$stmt = $conn->prepare("SELECT naam, prijs, beschrijving, id, image1 FROM salontafels");
$stmt->execute([]);
$rows = $stmt->fetchAll();
foreach ($rows as $row) : { ?>
<a class="formtitellink" href="productsalontafels.php?rowid=<?= $row['id'] ?>">
<?php } echo '
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top" src="data:image/jpeg;base64,' . base64_encode( $row['image1'] ) . '" />
<div class="card-body">
<h4 class="card-title">
' . $row['naam'] . '</a>
</h4><script> document.querySelector(\'p\').innerText = truncateText(\'p\', 100);</script>
<h5> Log in voor de prijs </h5>
<p class="card-text">' . $row['beschrijving'] . '</p>
</div>
<div class="card-footer">
<small class="text-muted">beschikbaar</small>
</div>
<!-- /.row -->
</div>
<!-- /.col-lg-9 -->
</div>'?>
Does anyone knows what is wrong?
Your problem is that document.querySelector('p') returns the first Element within the document that matches the specified selector. So it just returns the first p tag every time it's run.
This means that these two lines are your problem:
document.querySelector(\'p\').innerText = truncateText(\'p\', 100)
and
var element = document.querySelector(selector)
To fix it, you will need to select the specific element you're interested in each time
[If you want working code, rather than an answer to your question, I suggest just using substr($row['beschrijving'],0,100)]

MYSQL Product listing grid

So i am trying to pull product data from a database and have it listed out in a nice grid. I have got everything to work and its layout is very nice apart from one issue, the image URL isnt pulling properly:
<?php
while ($row = mysql_fetch_array($query))
$rows[] = $row;
foreach ($rows as $row){
$etitle = $row['title'];
$eprice = $row['price'];
$eurl = $row['prod_url'];
$eimage = $row['image'];
echo ('<div class="col-sm-6 col-md-4">
<div class="card">
<div class="card-image" style="background-image: url("' . $eimage . '");">
<div class="card-image-rating">');
echo ('£' . $eprice);
echo ('
</div><!-- /.card-image-rating -->
</div><!-- /.card-image -->
<div class="card-content">
<h2><a href="' . $eurl . '">');
echo $etitle;
echo ('</a></h2>
</div><!-- /.card-content -->
<div class="card-actions">
<i class="md-icon">favorite</i>
Show More
</div><!-- /.card-actions -->
</div><!-- /.card -->
</div><!-- /.col-* -->');
} ?>
In the source code and on page everything look good apart from their being no image. The column in the database has a URL of the image location in it but in the source code it appears to be removing the / from the path.
Any help would be amazing.
Thanks,
Based on your script, I noticed something goes wrong
<div class="card-image" style="background-image: url("' . $eimage .'");">
you use double quotation marks in style section
your html will look like this
<div class="card-image" style="background-image: url(" ");">
which is shouldn't be like that.
you have to use Apostrophe inside style, since u use quotation mark for style
your html should like this
<div class="card-image" style="background-image: url(' ');">
here, an example for you
https://jsfiddle.net/have_full/kass6ukr/

Select HTML with more than 1 pairs of tag by regex

<div class="apple">
<a href="..." > ... </a>
<div class="boy">
(some content here)
</div>
<div class="cat">
<b>Text One.</b> <br> <i>Text Two.</i>
</div>
<div class="dog">
<b>Text One.</b> <br> <i>Text Two.</i>
</div>
</div>
.
. (and there are couple more structure with cat class inside but not necessarily under the class apple)
.
<div class="zoo">
.
<div class="cat">
<b>Text One.</b> <br> <i>Text Two.</i>
</div>
.
</div>
.
.
.
I am working with PHP.
I want to know that how to select exactly "Text One." only from the div class="cat" the under div class="apple" out of the html (but not from any other).
Currnetly I am doing something like this:
$html=file_get_contents('xxx.html');
$a=preg_match_all("/\<div class\=\"apple\"(.*)\<div class\=\"cat\"\>(.*)<\/b\>/s",$html,$b);
foreach ($b[1] as $value) {
echo strip_tags("$value");
}
I just found it online, it may be possible but not be the best choice to due with the situation.
Many irrelevant content were also selected (i got everything within the last tag and more content than i want in )
please suggest me the appropriate regular expression or a better way to solve.
Since you mention a better way, I would suggest going with the simple html dom library, http://simplehtmldom.sourceforge.net.
In your example you would use it like this:
<?php
include 'simple_html_dom.php';
$html = str_get_html('<div class="apple">
<a href="..." > ... </a>
<div class="boy">
(some content here)
</div>
<div class="cat">
<b>Text One.</b> <br> <i>Text Two.</i>
</div>
<div class="dog">
<b>Text One.</b> <br> <i>Text Two.</i>
</div>
</div>
.
. (and there are couple more <div class="apple"> structure with cat class inside)
.
<div class="apple">
.
.
.
</div>
.
.
.');
$text = $html->find('div.cat b',0)->innertext;
print $text . PHP_EOL;
// it will print this
// Text One.

Categories