issue when ifisset added to code - php

some explenation
http://koopjesidee.nl/images/6755185029021696.jpg
in my database i have a column named new_label
when its a new product its set as new
when no new product the colums is empty
i want the class="main-label new-label" to be disabled when there nothing in that column
now as you see from the image, there is always a orange cirkel (as set in class main-label) even when column is empty
This is my code
<!-- select products from database -->
<?php
$query = "select * from contentwhere cat = 4 order by rand() LIMIT 18";
$result = $db->query($query);
while($row = $result->fetch_array())
{
$url =detail($row);
<!-- content on main page -->
echo '
<div class="item">
<div class="product-item">
<div class="main-label new-label"><span>'.$row['new_label'].'</span></div>
<div class="product-image"><img src="'.$row['picture_big'].'" alt="'.$row['name'].'"></div>
<div class="product-item-details">
<div class="product-item-name"> '.$row['name'].' </div>
<div class="price-box"> <span class="price">€'.$row['price'].'</span> <del class="price old-price">'.$row['from_price'].'</del> </div>
</div>
</div>
</div>';
}?>
I want add if isset like this so when there is no text in class=new-label class main-label new-label is disabled column in my database is new_label
<div class="item">
<div class="product-item">
<!-- ISSUE -->
if (isset($_row['new_label']!='')) {
<div class="main-label new-label"><span>'.$row['new_label'].'</span></div>;
} else {
<div class="main-label new-label"><span></span></div>;
}
<!-- HTTP ERROR 500 -->
<div class="product-image"><img src="'.$row['picture_big'].'" alt="'.$row['name'].'">

You cannot use isset() on the result of an expression, it can only be a variable. In fact, it will result in a fatal error if you try (which is probably why you received a 500 server error).
So you can probably change this line:
if (isset($_row['new_label']!='')) {
To this:
if (isset($_row['new_label'])) {
isset() returns a boolean anyway, so it doesn't make sense to compare it to a string.
Read more: http://php.net/manual/en/function.isset.php

SOLVED MYSELF GUYS
'.$row['new_label'].'';} ?>
thanks anyway

Related

How to do "If [value] === [id]" in php

I want to make my code change an element if that element's id matches the value of the button.
as in:
if "button value" === "div id".
I'm rusty on PHP though and don't know how to do it. The html looks a bit weird because its a sql injection site, though I'm pretty sure that shouldn't mess with anything.
Relevant PHP:
<?php
$ariabool="true";
$gridclass ="gridhidden";
if(isset($_POST['expand'])){
if(isset($_POST['expand'])==='id'){ //first pass of this, it doesn't work but i don't know how to do it properly
$gridclass = 'gridexpanded';
$ariabool = 'false';
}
}
?>
Relevant HTML
<div class='kagrid' >
<div class='griddisplay'>
<img src={$row['img']} />
<h2>{$row['name']}</h2>
<div>{$row['primpro']}</div>
<a href={$row['src']}>Image Source</a>
<button type='button' class='expand' name='expand' value={$row['id']}></button> //button i want to compare the value of
</div>
<div id={$row['id']} class='$gridclass' aria-hidden='$ariabool'> //element i want to compare the id of
<div class='gridcell bold'>Nicknames</div>
<div class='gridcell'>{$row['nicknames']}</div>
<div class='gridcell bold'>Pronouns</div>
<div class='gridcell'>{$row['pronouns']}</div>
<div class='gridcell bold'>Typical Age</div>
<div class='gridcell'>{$row['age']}</div>
<div class='gridcell bold'>Source(s)</div>
<div class='gridcell'>{$row['source']}</div>
<div class='gridcell bold'>Role(s)</div>
<div class='gridcell'>{$row['role']}</div>
<div class='gridcell bold'>Terms</div>
<div class='gridcell'>{$row['terms']}</div>
<div class='bold desc gridcell'>Description</div>
<div class='desc gridcell'>{$row['description']}</div>
</div>
</div>
This is one way of doing it.
The coalescing operator ?? returns a blank string if the POST value is undefined.
<?php
$ariabool=true;
$gridclass ="gridhidden";
$element = $_POST ['expand'] ?? '';
if($element === 'id')
{
$gridclass = 'gridexpanded';
$ariabool = false;
}
?>

PHP [ sorting data from database as collapsible bootstrap 4 cards ] [duplicate]

This question already has answers here:
List records grouped by category name
(2 answers)
Closed 2 years ago.
As the title says i'm developing shopping cart for a website and the cart contains all the products that been ordered from different companies like this :- screenshot
so as you see i'm trying to sort all products that is from the same company underneath each other as the specific company bill my question how can i accomplish that ?
what i have tried :-
nothing to mention actually i'm really so confused here i don't now what i'm going to (loop for or something like that .. )
i hope i explained what i want to accomplish, if not (screenshot)
code :- php PDOquery
<?
$accountid = '8';
require_once '..\Config.php';
// WHERE chemicalcom='$variable' OR name='$variable'
$dbCon = "mysql:host=$host;dbname=$db_name";
$variable = "Efexor";
$PDOCon = new PDO($dbCon, $username, $password);
$query = $PDOCon->prepare("SELECT * FROM basket WHERE accountid ='$accountid'");
$query->execute();
$basketItems = $query->fetchAll();
?>
code :- index.php
<? foreach($basketItems as $item){
echo'<h3>Bill('.$item['companyexported'].')</h3>
<div class="card">
<div class="row">
<div class="col-6"><img src="'.$item['imgpath'].'" class="productimg" alt=""></div>
<div class="col-auto">
<div class="card-title">
<div class="row">'.$item['name'].'</div>
<div class="row">'.$item['chemicalcom'].'</div>
<div class="row">'.$item['concentration'].'</div>
<br>
<div class="row">'.$item['price'].' $
</div>
<span class="badge badge-info qty">'.$item['qty'].'</span>
</div>
</div>
</div>
</div>';}?>
Thanks .
First, you will need to update your sql to order by the company, that will make the php simpler.
SELECT * FROM basket WHERE accountid ='$accountid' order by companyexported
then in your php, you only want to show the company once, so you create an empty variable and check it. If that variable doesn't contain a matching company name, show the company and set that variable to the current company name.
<?
$curr_co = "";
foreach($basketItems as $item){
if($curr_co != $item['companyexported']){
echo '<h3>Bill('.$item['companyexported'].')</h3>';
$curr_co = $item['companyexported'];
}
echo '<div class="card">
<div class="row">
<div class="col-6"><img src="'.$item['imgpath'].'" class="productimg" alt=""></div>
<div class="col-auto">
<div class="card-title">
<div class="row">'.$item['name'].'</div>
<div class="row">'.$item['chemicalcom'].'</div>
<div class="row">'.$item['concentration'].'</div>
<br>
<div class="row">'.$item['price'].' $
</div>
<span class="badge badge-info qty">'.$item['qty'].'</span>
</div>
</div>
</div>
</div>';}?>

how to call columns from database and format it with HTML code

Actually I am beginner programmer in HTML, CSS and PHP. I have simple website for add and register in courses. The user should be add course into website and the courses should be posted on the site.so users can browse and register.
Actually my problem is how to call the course name from database and how to format it with HTML code as I want.
This is the page of courses which is content the list of available courses in website ( please note it is only HTML code, I do that to see how the page will be )
Screenshot of page:
So as you see, the first page include many this HTML code to add course list into website with the following code:
<div class="card card-1">
<a href="http://127.0.0.1/project2/course details/course1.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% "></a> <div class="container">
<h4 class="textstyle"><b>Operating System</b> </h4>
<p class="textstyle">Free Course</p>
</div>
</div>
what i want do with PHP?
I want to write a PHP code to replace the P and h4 with the course name, cost of courses from my database for each available course.
Please note: the pic for each course it will be from my pc, no need to call the pic from database.
I tried to write PHP code like below:
<div>
<div class="card card-1">
<a href="http://127.0.0.1/project2/course details/course1.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% "></a> <div class="container">
<?php
include_once("db.php");
$result = mysqli_query(OpenCon(), "SELECT Course_Name,cost FROM `course`");
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
while($res = mysqli_fetch_array($result)) {
echo "<p>".$res['Course_Name']."</p>";
echo "<p>".$res['cost']."</p>";
}
?>
</div>
</div>
</div>
This is my result:
It's okay but I want the style to be like the first screenshot. each course should have picture.
After that when the user click on course name. I want move to another page which is content the course details ( for the same course that user clicked ) also it's called for my database
like this:
I hope any one help my to solve this problem only, I should solve this problem within 2 days only. and sorry if my explanation is bad.
Thanks in advance for everyone.
Put the code in a PHP loop.....
So, this
<div class="card card-1">
<a href="http://127.0.0.1/project2/course details/course1.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% ">
</a>
<div class="container">
<h4 class="textstyle"><b>Operating System</b> </h4>
<p class="textstyle">Free Course</p>
</div>
</div>
Becomes (after cleaning up the code a bit - I think you didn't mean to use two <p> in there, but I left them so you can see it. Note that using different lines for the segments makes it a lot easier to see what you have.)
include_once("db.php");
$result = mysqli_query(OpenCon(), "SELECT Course_Name,cost FROM `course`");
$count = 0;
while($res = mysqli_fetch_array($result)) {
$count ++;
// NOTE: Here is the LOOP! - not outside the query, but INSIDE it
// First you 'jump out' of PHP, going back to HTML
?> <!-- now you are in HTML (when you need PHP again, you 'jump in' and 'jump out' as needed - see the code below....) -->
<div class="card card-<?php echo $count;?>">
<a href="http://127.0.0.1/project2/course details/course<?php echo $count;?>.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% ">
</a>
<div class="container">
<h4 class="textstyle">
<b><p><?php echo $res['Course_Name'];?></p></b>
</h4>
<p class="textstyle">
<p><?php echo $res['cost'];?></p>
</p>
</div>
</div>
<?php // we are in PHP again....
}
That should do what you asked for - though I would go a step (well, more than one...) further and make as much of this dynamic as you can.
For this I will presume that:
your database table has a column called 'id' (if it doesn't, you should have) and it relates to the course number (you could make a course number column if they don't match up, but I'm keeping it simple)
you have all your pictures labeled 'coursepicX' where the X is the course number.
We'll use 'coursepic' as a default in case there isn't a picture yet...
Now, the code is more dynamic!
include_once("db.php");
$result = mysqli_query(OpenCon(), "SELECT id,Course_Name,cost FROM `course`");
while($res = mysqli_fetch_array($result)) {
// NOTE: Here is the LOOP! - not outside the query, but INSIDE it
// First you 'jump out' of PHP, going back to HTML
?> <!-- now you are in HTML (when you need PHP again, you 'jump in' and 'jump out' as needed - see the code below....) -->
<div class="card card-<?php echo $res['id']?>">
<a href="http://127.0.0.1/project2/course details/course<?php echo $res['id']?>.php">
<?php
$pic = "http://127.0.0.1/project2/icons/coursepic.jpg";
if(file_exists("http://127.0.0.1/project2/icons/course" . $res['id'] . ".jpg") {
$pic = "http://127.0.0.1/project2/icons/course" . $res['id'] . ".jpg";
}
<img src="<?php echo $pic; ?>" alt="Avatar" style="width:101% ">
</a>
<div class="container">
<h4 class="textstyle">
<b><p><?php echo $res['Course_Name'];?></p></b>
</h4>
<p class="textstyle">
<p><?php echo $res['cost'];?></p>
</p>
</div>
</div>
<?php // we are in PHP again....
}
Note that this is the basic 'shopping cart' sort of program - you will likely use it many (many) times in your career.
Happy Coding!

Make all title elements the same height when some titles are longer than others and need to wrap

I am displaying information in a card-based layout, but if any title in the card is longer than 41 characters, it doesn't fit into the card.
I thought about using [wordwrap], but all that would do is cause the one specific title to wrap and change the height of that card, affecting the layout of the other cards.
This is the PHP I use to print my results for pagination purposes, so it only prints 9 things at a time.
<div class="row">
<?php
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<div class="col xl4 l4 m12 s12">
<div class="card z-depth-5">
<div class="card-content">
<p><? echo $row["title"]; ?></p>
<p>Category: <? echo $row["category"]; ?></p>
</div>
</div>
</div>
<?php
};
?>
</div>
How would I go about line breaking every title if even one title is detected as longer than 41 characters?
EDIT: This is the solution I created:
$titlebreak = $row["title"];
if (strlen($titlebreak) >= 40)
{
$titlebreak2 = wordwrap($titlebreak, 39, "</p><p>");
}
else
{
$titlebreak2 = $row["title"] . "<p> </p>\n";
}
I've included 3 possible solutions below - adding manual line breaks as you asked about in your question; a basic but unsatisfactory CSS option and a jQuery solution which in this case is the one I would suggest as the most flexible.
Although a CSS-only solution is usually the preferred way of fixing a layout issue, when it comes to equal heights of elements there isn't a clear-cut way to do it and often a jQuery solution like the one below is required.
Manual line-break - as requested in your question
Instead of doing an additional SQL query as mentioned, you can easily do it in the PHP in 2 different ways:
(a) loop through the rows before displaying them to calculate the title lengths, then loop again to display with/without the line break
or
(b) if you really down't want to loop twice, you could include the line break regardless of length as you loop once, but also calculate the line length in that loop. Then hide the line break using CSS if its not required
(a) 2 loops: Calculate length to determine whether to add the line break or not:
<?php
$maxchars = 41;
$cards = array();
$bLongTitle = false;
while ($row = mysql_fetch_assoc($rs_result)) {
// save detaisl to $cards array
$cards[$row["title"]] = $row["category"];
// check title lengths until we find one over 41 - no need to check any more after that
if (!$bLongTitle && strlen($row["title"])) > $maxchars)
$bLongTitle = true;
}
?>
<div class="row">
<?php
foreach ($cards as $title => $category) {
?>
<div class="col xl4 l4 m12 s12">
<div class="card z-depth-5">
<div class="card-content">
<p><?
// if there were any long title, wrap them all text
if ($bLongTitle)
$title = wordwrap($title, $maxchars, "<br />\n");
echo $title;
?></p>
<p>Category: <? echo $category; ?></p>
</div>
</div>
</div>
<?php
}
?>
</div>
(b) 1 loop: Always display line break, and hide it if not required
<div class="row">
<?php
$bLongTitle = false;
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<div class="col xl4 l4 m12 s12">
<div class="card z-depth-5">
<div class="card-content">
<p class="cardTitle"><? echo wordwrap($title, $maxchars, "<br />\n"); ?></p>
<p>Category: <? echo $row["category"]; ?></p>
<?php
// check title lengths until we find one over 41 - no need to check any more after that
if (!$bLongTitle && strlen($row["title"])) > $maxchars)
$bLongTitle = true;
?>
</div>
</div>
</div>
<?php
};
?>
</div>
<?php if ($bLongTitle) {?>
<script>.cardTitle br {display:none;}</script>
<?php } ?>
CSS-only solution - not viable for the OP?
Because the titles are aren't direct siblings, the only way would be to fix the height of all title. This isn't a desirable solution, as I'm sure titles can vary a lot in length so its impossible to pick a "default" height to suit every possibility, and even that's complicated by the responsive width of the columns potentially changing the heights dynamically.
But for the sake of completeness:
add a class (e.g. .cardTitle) to the title in your loop
identify suitable heights for the title with and without a line break, and set these in your CSS
add the corresponding class (e.g. wrapTitle) to your <p> if any title is too long in a loop (similar to adding a line break above)
CSS
p.cardTitle { height:20px; } /* example of the default height for title */
p.cardTitle.wraptitle { height:40px; } /* example of height for wrapped title */
PHP (after looping through SQL rows to fill $cards array as option (a) above)
<?php
foreach ($cards as $title => $category) {
?>
<div class="col xl4 l4 m12 s12">
<div class="card z-depth-5">
<div class="card-content">
<p class="cardTitle <?php if ($bLongTitle) echo "wrapTitle"; ?>"><? echo $title; ?></p>
<p>Category: <? echo $row["category"]; ?></p>
</div>
</div>
</div>
<?php
};
?>
jQuery
You could use jQuery to loop through all elements to calculate the heights, and set them all to the tallest.
You could write the code yourself (see How to make all div columns of the same height automatically with jQuery or Setting equal heights for div's with jQuery but there is a library available to do this for you: matchHeight
Using the library, all you need to do is include it on your page and call it like this (assuming you've added the class cardTitle to the <p> that holds your title)
jQuery(document).ready(function() {
$(".cardTitle").matchHeight();
});
I would perform a new SQL query first to see if any results have a title length of greater than 41 characters:
select count(*) from table where length(title)>41
And then set the result of this query to be a variable, e.g. $has41
You can then use an if statement within your loop ...
if($has41) {
// do something with $row['title']
} else {
// do something else with $row['title']
}

Add a unique style class to each mySQL entry on page?

I have a php function to retrieve the three most recent entries to a table that holds news highlights. Here is my function:
function getHighlights(){
return $this->query("
SELECT *
FROM `highlights`
ORDER BY `inserted` DESC
LIMIT 3
");
}
These highlights are then placed on my homepage via a foreach loop. Here is my code:
<?php foreach($highlights as $a){ ?>
<div class="col-md-6 highlight">
<div class="highlightItem">
<img class="highlight-backdrop" src="<?=$a->backdrop;?>">
<p class="highlight-title"><?=$a->title;?></p>
</div>
</div>
<?php } ?>
I'd like for my homepage to ALWAYS be in this format:
News Highlight Format. However, the only reason it's in that format right now is because the images have predefined sizes that fit nicely together.
I want to be able to reference the most recent news highlight and set that image to always display as 300 x 210. I also want to reference the next two highlights and set them to always display as 300 x 100.
What's the best course of action for this?
<?php foreach($highlights as $index => $a){ ?>
if($index == 0){
<div class="col-md-6 highlight">
<div class="highlightItem">
<img width="300" height="210" class="highlight-backdrop" src="<?=$a->backdrop;?>">
<p class="highlight-title"><?=$a->title;?></p>
</div>
</div>
}else{
<div class="col-md-6 highlight">
<div class="highlightItem">
<img width="300" height="100" class="highlight-backdrop" src="<?=$a->backdrop;?>">
<p class="highlight-title"><?=$a->title;?></p>
</div>
</div>
}
<?php } ?>
try to use that code

Categories