How to do "If [value] === [id]" in php - 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;
}
?>

Related

issue when ifisset added to code

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

While cycle result to three columns

I have following HTML code and I need make results from db, but I don't know how. There are three static divs. I don't know how to end static div and how to recognize columns. Can you help me please? Please see structure of html code with numbers of results. Thank for help.
<div class="static-div">
<div class="first-div">1</div>
<div class="second-div">4</div>
<div class="third-div">7</div>
<div class="first-div">10</div>
<div class="second-div">13</div>
<div class="third-div">16</div>
<div class="first-div">19</div>
<div class="second-div">22</div>
<div class="third-div">25</div>
</div>
<div class="static-div">
<div class="first-div">2</div>
<div class="second-div">5</div>
<div class="third-div">8</div>
<div class="first-div">11</div>
<div class="second-div">14</div>
<div class="third-div">17</div>
<div class="first-div">20</div>
<div class="second-div">23</div>
<div class="third-div">26</div>
</div>
<div class="static-div">
<div class="first-div">3</div>
<div class="second-div">6</div>
<div class="third-div">9</div>
<div class="first-div">12</div>
<div class="second-div">15</div>
<div class="third-div">18</div>
<div class="first-div">21</div>
<div class="second-div">24</div>
<div class="third-div">27</div>
</div>
You can achieve this output by using Arrays.
we will create three arrays for given three static divs.
try the following code.
The variable $i is not for loop iteration so if you are using $i for iteration please replace the following $i with other name.
$i=1;
$first_arr=array();
$sec_arr=array();
$third_arr=array();
while(database loop condition)
{
if($i==1)
{
$val=' <div class="first-div">'.$row['column'].'</div>';//$val is a variable from database query result
array_push($first_arr,$val);
$i++;
}
elseif($i==2)
{
$val=' <div class="second-div">'.$row['column'].'</div>';//$val is a variable from database query result
array_push($sec_arr,$val);
$i++;
}
elseif($i==3)
{
$val=' <div class="second-div">'.$row['column'].'</div>';//$val is a variable from database query result
array_push($sec_arr,$val);
$i=1;
}
}
$first_div=implode('',$first_arr);
$sec_div=implode('',$sec_arr);
$third_div=implode('',$third_arr);
echo '<div class="static-div">'.$first_div.'</div>
<div class="static-div">'.$sec_div.'</div>
<div class="static-div">'.$third_div.'</div>';

Include php is throwing result in header

I am trying to use the php code to display a comment box on a page:
$object_id = 'article_12';
include('commentanything/php/loadComments.php');
For some reason the comment box is not appearing where i want it to appear,it keeps throwing the comment box to header, is there any way i can make it appear exactly where i want it to?, here is where i am trying to use it(not a complete code):
<!--<h1 align="center" style="font-size:1.2em"><strong>'.$row['title'].'</strong></h1>-->
<div class="song-art"><img src="'.$this->url.'/thumb.php?src='.$row['art'].'&t=m&w=112&h=112" id="song-art'.$row['id'].'" title="'.$row['title'].'" alt="'.$row['title'].'"/></div>
<div class="song-top">
<div class="song-timeago">
<a href="'.$this->url.'/mp3download/'.$row['id'].'/'.$this->genSlug($row['title']).'.html"><span id="time'.$row['id'].'">
<div class="timeago'.$b.'" title="'.$time.'">
'.$time.'
</div>
</span>
</a>
</div>
<div data-track-name="'.$row['name'].'" data-track-id="'.$row['id'].'" id="play'.$row['id'].'" class="track song-play-btn">
</div>
<div class="song-titles">
<div class="song-author"><a onmouseover="profileCard('.$row['idu'].', '.$row['id'].', 0, 0);" onmouseout="profileCard(0, 0, 0, 1);" onclick="profileCard(0, 0, 1, 1);" href="'.$this->url.'/index.php?a=profile&u='.$row['username'].'">'.realName($row['username'], $row['first_name'], $row['last_name']).'</a></div>
<div class="song-tag">
'.$tag.'
</div>
<div class="song-title">
<div class="track-link"><h2 style="font-size:1.0em">
<!--<div id="song-name'.$row['id'].'">'.$row['title'].'</div>-->
</h2></div>
</div>
</div>
</div>
<div class="player-controls">
<div id="song-controls'.$row['id'].'">
<div id="jp_container_123" class="jp-audio">
<div class="jp-type-single">
<div class="jp-gui jp-interface">
<div class="jp-progress">
<div class="jp-seek-bar">
<div class="jp-play-bar"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="track-actions-container">
</div>
'.$extra_links.'
'.$seo_links.'
'.$comment.'
'.$charts.'
</div>';
$object_id = 'article_12';
include('commentanything/php/loadComments.php');
$start = (isset($row['extra_id'])) ? $row['extra_id'] : $row['id'];
}
}
I am trying to display it right after '.$charts.' but it keeps showing up in header
it's looking like you haven't worked to match HTML in index and loadComments.php .
first copy your loadComments.php code in place of include('commentanything/php/loadComments.php');
and check for html/css errors. It's there.
And if it failed too, your php/html nesting is wrong which i cant tell because u don't have even used to know us how and where u are stuffing php. You simply added variables in above displayed code

Php search function displaying null

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.

php, jquery only first id working normal

I have the following code in .php (code is edited for easier understanding).
while($row = mysql_fetch_array($biznis)){
<div id='listItem'>
<div id='listPic'>
<img src='../../social/images/avatar/empty_avatar_full.jpg'></img>
</div>
<div id='listCont'>
<span>abcde<i>(request pending)</i></span>
</div>
<div id="listInfo">
<span id='info'>More info</span>
</div>
<div style='clear:both;'></div>
<div id='moreInfo'>
<span>Invitation sent: xx-yy-zzzz</span>
</div>
</div>
<div style='clear:both;'></div>
}
So the code is nothing special, I just get all records from the table lined up.
So now, here is the problem, as you can see, I use the same ids for the same elements.
Jquery code
$(document).ready(function () {
$("#moreInfo").hide();
$("#info").click(function(){
$("#moreInfo").slideToggle();
});
});
OK so here are now the problems. Only first div (#moreinfo) is hidden, all the others are shown. And only first span (#info) is toggling the slider.
I tried with putting the counter for ids, and jquery each function, but nothing realy worked as i imagined.
Ty, Sebastian
Element IDs should be unique identifiers for that element. Use classnames for generic selectors:
<? while($row = mysql_fetch_array($biznis)): ?>
<div class="listItem">
<div class="listPic">
<img src="../../social/images/avatar/empty_avatar_full.jpg"></img>
</div>
<div class="listCont">
<span>abcde<i>(request pending)</i></span>
</div>
<div class="listInfo">
<span class="info">More info</span>
</div>
<div style="clear:both;"></div>
<div class="moreInfo">
<span>Invitation sent: xx-yy-zzzz</span>
</div>
</div>
<div style="clear:both;"></div>
<? endwhile; ?>
And change your jQuery to:
$(function() {
$('.moreInfo').hide();
$('.info').click(function() {
$('.moreInfo').slideToggle();
});
});
Edit
$('.info').click(function() {
$(this).parent().siblings('.moreInfo').slideToggle();
});
use class and not ids because you're in a while loop. An id exists only once.
And update your JS.

Categories