if stamement not working against db query - php

I am running the code below but can't get the if statement to work. I want to be able to remove the div if there is no content and if there is then to display it but at the moment it either removes it all if one db entry is empty instead of just removing the 1 empty row.
<?php
$con = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
extract($_GET);
{ $result = mysqli_query($con,"SELECT * FROM links ORDER by id ");
while($row = mysqli_fetch_array($result)) {
if ($section=""){
echo"";
}
elseif ($section="a"){
echo"<div class=\"page-header\">" . $row['section'] . "\</div>";
}
echo "<div class=\"links-wrap\">
<div class=\"links-box\">
<div class=\"links-image\"><img src=\"images/links/" . $row['img1'] . "\"></div>
<div class=\"links-site-txt\">" . $row['link1'] . "</div>
<div class=\"links-desc\">" . $row['desc1'] . "</div>
</div>
<div class=\"spacer-left\"></div>
<div class=\"links-box\"></div>
<div class=\"spacer-left\"></div>
<div class=\"links-box\"></div>
</div>";}}
mysqli_close($con);
?>

<?php
$con = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
extract($_GET);
{ $result = mysqli_query($con,"SELECT * FROM links ORDER by id ");
while($row = mysqli_fetch_array($result)) {
if ( $section == "" ){ //double equality for condition check
echo "";
}
else if ($section == "a" ){
echo"<div class=\"page-header\">" . $row['section'] . "\</div>";
}
echo "<div class=\"links-wrap\">
<div class=\"links-box\">
<div class=\"links-image\"><img src=\"images/links/" . $row['img1'] . "\"></div>
<div class=\"links-site-txt\">" . $row['link1'] . "</div>
<div class=\"links-desc\">" . $row['desc1'] . "</div>
</div>
<div class=\"spacer-left\"></div>
<div class=\"links-box\"></div>
<div class=\"spacer-left\"></div>
<div class=\"links-box\"></div>
</div>";}}
mysqli_close($con);
?>

To compare value by if or elseif == is required not =. = generally use to assign value to a variable. Change your codes like below.
$con = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
extract($_GET);
$result = mysqli_query($con,"SELECT * FROM links ORDER by id ");
while($row = mysqli_fetch_array($result)) {
$section = $row['section'];
if ($section == ""){
echo "";
}
elseif ($section == "a"){
echo"<div class=\"page-header\">" . $row['section'] . "\</div>";
}
echo "<div class=\"links-wrap\">
<div class=\"links-box\">
<div class=\"links-image\"><img src=\"images/links/" . $row['img1'] . "\"></div>
<div class=\"links-site-txt\">" . $row['link1'] . "</div>
<div class=\"links-desc\">" . $row['desc1'] . "</div>
</div>
<div class=\"spacer-left\"></div>
<div class=\"links-box\"></div>
<div class=\"spacer-left\"></div>
<div class=\"links-box\"></div>
</div>";
}
mysqli_close($con);

Related

How to select a result from a result that return by a SQL

I'm using SQL and PHP to pull information from my database and it returned a list of users that met the criteria. And I need to redirect to another page using the selected amount from the picture below. How can I grab this amount to another page?
For example, when I click the first select, I can go to the other page that showing $24, and Rubinsztein Abdel, 182 Crownhardt Lane...
<!--loader end-->
<!-- Main -->
<div id="main">
<!-- wrapper -->
<div id="wrapper">
<div class="content">
<!-- Map -->
<div class="map-container column-map right-pos-map">
<div id="map-main"></div>
<ul class="mapnavigation"></ul>
<div class="scrollContorl mapnavbtn" title="Enable Scrolling"><span><i class="fa fa-lock"></i></span></div>
</div>
<!-- Map end -->
<!--col-list-wrap -->
<div class="col-list-wrap left-list">
<!-- list-main-wrap-->
<div class="list-main-wrap fl-wrap card-listing">
<!-- listing-item -->
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$severname = "localhost";
$username = "root";
$password = "";
$dbname = "dbn";
//$conn = mysqli_connect('xx', 'xx', 'xx','xx');
$conn = mysqli_connect('dxx', 'xx', 'xx','xx');
//check connection
if (mysqli_connect_errno())
{echo nl2br("Failed to connect to MySQL: ". mysqli_connect_error() . "\n"); }
else
{ echo nl2br("");}
$city = mysqli_real_escape_string($conn, $_POST['city']);
$date1 = mysqli_real_escape_string($conn, $_POST['date1']);
$date2 = mysqli_real_escape_string($conn,$_POST['date2']);
$pet = mysqli_real_escape_string($conn, $_POST['pet']);
//var_dump($date2);
$sql = "SELECT CONCAT(s.lname, ' ', s.fname) AS fullName, s.ratings, s.rate, s.phone, s.address FROM Pet_Sitter AS s WHERE s.citystate='".$city."' AND s.pet_type='".$pet."' "
;
//echo $sql."<br/>";
$result = mysqli_query($conn,$sql);
if (!$result) {
printf("Error: %s\n", mysqli_error($conn));
exit();
}
if ($result=mysqli_query($conn,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
printf("");
}
//var_dump($result);
$num_rows = mysqli_num_rows($result);
?> <div class="container"> <?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//var_dump($row);
// echo "<tr><td>" . $row['fullName'] . " </td><td>" . $row['ratings'] . " </td><td>" . $row['rate'] . " </td></tr>";
// echo '<div class="listing-item">';
echo '<div class="listing-item">';
echo '<article class="geodir-category-listing fl-wrap">';
echo '<div class="geodir-category-img">';
echo '<img src="../images/all/1.jpg" alt="">';
echo '<div class="overlay"></div>';
echo '<div class="list-post-counter"><span>' . $row['rate'] . '</span></div>';
echo '</div>';
echo '<div class="geodir-category-content fl-wrap">';
echo '<a class="listing-geodir-category" href="../index.html">Pet sitting</a>';
echo '<h3>' . $row['fullName'] . '</h3>';
echo '<p>PET-SITTER INFORMATION</p>';
echo '<div class="geodir-category-options fl-wrap">';
echo '<span>' . 'Reviews ' . $row['ratings'] .'</span>';
echo '<div class="geodir-category-location">'. $row['address'] . " ยท " . $row['phone'] .
'</div>';
echo '</div>';
echo '</div>';
echo '</article>';
echo '</div>';
}?>
<?php
} else {
echo "0 results";
}
mysqli_close($conn);
?>
</div>
</div>
An easy way to do it is to create one page (the one you wanna go if you click on your items) and use GET on URL to get the page id;
On your actual page you will use a link like :
<a href="new-page.php?id=<?php echo $row['id']; ?>"
Here the ?id= on URL is a parameter, that you can get on the other page :
$pageId = $_GET['id'];
Then, you just have to make a SQL request
SELECT * FROM Pet_Sitter WHERE id=$pageId
(better to use prepare and execute for this request as the value of $pageId can be change by everybody by changing the URL).
So every time you click on a link, the page will display the matching informations.
Also you should closed <?php when you are writing HTML instead of using a lot of echo.

Database query - how to put the result in the HTML Code?

I have a database where the path of pictures are stored. Now I want to get the path of the picture and the description but I dont know how to surround the data with HTML-Code. A "(Html-Code) " indicates Html-Code but in my example it is a bit more complicated. Maybe it is wrong the way I want to do it but I dont know another way.
Here the Php-Code
<?php include ("data_con.php");
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Dateipfad FROM last_upload WHERE ID = 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc(); {
}
} else {
echo "0 results";
}
$conn->close();
echo "Connected successfully";?>
Here the Html-Code
<div class="col-xs-5 col-sm-5 col-md-5 col-lg-5 no-gutter">
<div class="rahmen"> <img src="Pic/OP.jpg" class="img-fluid" alt="Responsive image">
<p>Episode 1</p></div>
First of all I just want to have the path of the file (Dateipfad) ecchoed in
<img src="Pic/Op.jp">
therefore I need to make it like that
<img="echo .row[Dateipfad]."
But all the " to indicate the html code is really hard.
The code have to be in
$row = $result->fetch_assoc(); {
}
but with all the HTML-Code it is really hard and doesnt work for me. I think i do it completly wrong. Can anyone help me?
Method 1
<?php $conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Dateipfad FROM last_upload WHERE ID = 1";
if ( $result = $conn->query($sql) ) {
while ($row = mysqli_fetch_row($result)){
echo '<img src ="$row['value']">'$row['value']'</img>';
}
} else {
echo "0 results";
}
$conn->close();
echo "Connected successfully";
Method Two
<?php $conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Dateipfad FROM last_upload WHERE ID = 1";
if ( $result = $conn->query($sql) ) {
while ($row = mysqli_fetch_row($result)){ ?>
// write html here
<ul>
<li>Just a test</li>
<li>Another test</li>
</ul>
<img src ="<?php echo $row['link']; ?>"><?php echo $row['value']; ?></img>
<?php }
} else {
echo "0 results";
}
$conn->close();
echo "Connected successfully";
Actually what you are looking for is something akin to
<img src="<?= $row[Dateipfad] ?>" />
Also, are you sure that you don't want to iterate on all the results? Will your query always return a single result or none at all?
<img src="<?php echo row[Dateipfad]; ?>" class="img-fluid" alt="Responsive image">
And don't use short handed echo statement <?= ?> for my sake.
update
if($result->num_rows > 0)
{
while ($row = mysqli_fetch_row($result)){?>
<div class="col-xs-5 col-sm-5 col-md-5 col-lg-5 no-gutter">
<div class="rahmen"> <img src="<?php echo $row['Dateipfad']; ?>" class="img-fluid" alt="Responsive image">
<p>Episode 1</p>
</div>
</div>
<?php}
}

How to create jquery tabs using php?

I used jquery-ui tabs using php but it's not working, I am unable to identify the problem, please help me.
<div id="tabs">
<div class="row">
<div class="col-md-4">
<?php
$con = mysqli_connect("localhost", "admin", "123456", "gazette");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM gazette_details");
while ($row = mysqli_fetch_array($result)) {
echo "<ul>";
$id = '#tabs-' . $row['id'];
echo "<li>" . "<a href='$id'>" . $row['gazette_id'] . "</a>" . "</li>";
echo "</ul>";
}
mysqli_close($con);
?>
</div>
<div class="col-md-4">
<div id="tabs-12">1</div>
<div id="tabs-13">2</div>
<div id="tabs-14">3</div>
</div>
</div>
<div class="col-md-4">.col-md-4</div>
</div>
I used this website example: http://jqueryui.com/tabs/
you may try
<?php
$ARR_RESULT = array();
$con = mysqli_connect("localhost", "admin", "123456", "gazette");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM gazette_details");
while ($row = mysqli_fetch_array($result)) {
$ARR_RESULT[] = $row;
}
mysqli_close($con);
?>
<div id="tabs">
<ul>
<?php
foreach($ARR_RESULT as $arr)
{
echo '<li>'.$arr['gazette_id'].'</li>';
}
?>
</ul>
<?php
foreach($ARR_RESULT as $arr)
{
echo '<div id="tabs-'.$arr['id'].'">';
echo '<p>Tab-'$arr['id'].'</p>';
echo '</div>';
}
?>
</div>

multiple inputs - Problems with array

it is such that I need to use multiple inputs on my side,
I've tried to do like this but it does not work as it will not show up on the page at all in some manner.
HTML
<div id="onlinetestside">
<div id="onlinetestsidealt"><?php echo $ordet;?></div>
<input type="text" name="ord[]" maxlength="190">
<div style="clear:both;"></div>
</div>
PHP
//ordetalt its coming from database.
$alt = $_POST["ord"];
if($ordetalt == $alt)
{
echo $ordetalt . " og " . $alt;
}
else
{
echo "Error " . $ordetalt . " and " . $alt;
}
error appears like this:
Error Hej and Array
What I want to get to it shall be such that "$ordetalt == $alt" have the same content and it fits together.
EIDT
Here I show the entire code where I need to download some code to the side.
if ($stmt = $this->mysqli->prepare('SELECT id, ordet, ordetalt FROM test WHERE getid = ?')) {
$stmt->bind_param('i', $id);
$id = $_GET['id'];
$stmt->execute();
$stmt->bind_result($id, $ordet, $ordetalt);
while ($stmt->fetch()) {
?>
<div id="onlinetestside">
<div id="onlinetestsidealt"><?php echo $ordet;?></div>
<input type="text" name="ord[]" maxlength="190">
<div style="clear:both;"></div>
</div>
<?php
//ordetalt its coming from database
$alt = $_POST["ord"];
if($ordetalt == $alt)
{
echo $ordetalt . " og " . $alt;
}
else
{
echo "Error " . $ordetalt . " and " . $alt;
}
}
$stmt->close();
}
EIDT EIDT
$i = 0;
$a = $i++;
$alt = $_POST["ord"][$a];
if($ordetalt == $alt)
{
echo $ordetalt . " og " . $alt;
}
else
{
echo "Error " . $ordetalt . " and " . $alt;
}
Barmar is spot on, your line
$alt = $_POST["ord"];
Should read
$alt = $_POST["ord"][0];
OR
change your html like so:
<div id="onlinetestside">
<div id="onlinetestsidealt"><?php echo $ordet;?></div>
<input type="text" name="ord" maxlength="190">
<div style="clear:both;"></div>
</div>

Show all fields based on ID

I am building a blog and trying to show all comments that apply to the post.
Each post has an ID and each comment is stored with the post ID.
here is my code:
<?php
$con = mysql_connect("localhost","cl49-XXX-b","X");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cl49-XXX-b", $con)or die( "Unable to select database line 873");
$result=mysql_query("SELECT * FROM blogcomments WHERE ID='".$ID."'") or die('Error on Line 215 :'.mysql_error());
echo " <ul class='comments'> "; // first row beginning
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$name = $row['name'];
$email = $row['email'];
$comment = $row['comment'];
$created=$row['created'];
echo" <li>
<div class='comment'>
<div class='thumbnail'>
<img class='avatar' alt='' src='img/avatar.jpg'>
</div>
<div class='comment-block'>
<div class='comment-arrow'></div>
<span class='comment-by'>
<strong>$name</strong>
<span class='pull-right'>
<span> <a href='#'><i class='icon-reply'></i> Reply</a></span>
</span>
</span>
<p>$comment</p>
<span class='date pull-right'>$created</span>
</div>
</div> ";
echo " </li>"; // it's time no move to next row
}
?>
When I run this code the page only shows one comment, although my DB has 3 comments with the correct ID.
I would consider using mysqli_ as mysql_ has been depreciated. I'd also consider using a while loop, hopefully this will help:
<?php
$DBServer = 'localhost';
$DBUser = 'xxxx';
$DBPass = 'xxxx';
$DBName = 'xxxx';
$mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if ($mysqli->connect_errno) {
echo "Failed to connect to the database: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$query = "SELECT * FROM blogcomments WHERE ID='". $ID ."'";
echo " <ul class='comments'> ";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
?>
<li>
<div class='comment'>
<div class='thumbnail'>
<img class='avatar' alt='' src='img/avatar.jpg'>
</div>
<div class='comment-block'>
<div class='comment-arrow'></div>
<span class='comment-by'>
<strong><?php echo $row['name']; ?></strong>
<span class='pull-right'>
<span><a href='#'><i class='icon-reply'></i> Reply</a></span>
</span>
</span>
<p><?php echo $row['comment']; ?></p>
<span class='date pull-right'><?php echo $row['created']; ?></span>
</div>
</div>
</div>
</li>
<?php
} $result->close();
} $mysqli ->close();
echo "</ul>";
?>
I haven't tested this for bugs, but let me know if you like further information.
When you do this :
mysql_query("SELECT * FROM blogcomments WHERE ID='".$ID."'")
You select just one comment in the database.
Maybe you have to do that :
mysql_query("SELECT * FROM blogcomments WHERE post_ID='".$post_ID."'")
Because you are selecting the row where ID field value is $ID in the table blogcomments.
I guess you store the referred post_id to whom the comment is connected in a field called something like "post_id". You better point your select query to that field ;)
If you put the structure of the table in the question, I might help :)
You're not closing your <ul>. Might it just be a HTML formatting issue?

Categories