My While Loop is not grabbing the MySQL database entries - php

why isn't my while loop getting the mysql database entries and presenting them like demonstrated? Thanks :).
<?php $djs_all_db = mysql_query("SELECT * FROM djs")
or die(mysql_error());
$djs_all_num = mysql_num_rows($djs_all_db);
while($djs_all = mysql_fetch_array( $djs_all_db )) {
if ($djs_all_num % "2") {
echo "<div class=\"row\">
<div class=\"column column-2\">
<img src=\"images/about/" . $djs['username'] . "-profile.png\" alt=\"Profile\" class=\"profile-image\"/>
<p class=\"float-left\"><strong>" . $djs['realname'] . "</strong></p>
<p class=\"float-right\">" . $djs['position'] . "</p>
<div class=\"clear\"></div>
<p>" . $djs['biography'] . "</p>
</div>";
} else {
echo "<div class=\"column column-3\">
<img src=\"images/about/" . $djs['username'] . "-profile.png\" alt=\"Profile\" class=\"profile-image\"/>
<p class=\"float-left\"><strong>" . $djs['realname'] . "</strong></p>
<p class=\"float-right\">" . $djs['position'] . "</p>
<div class=\"clear\"></div>
<p>" . $djs['biography'] . "</p>
</div></div>";
} }?>

Your loop defines: $djs_all -- In your code you use: $djs['username'], change it to $djs_all['username']

Don't know if that's your issue, but
if ($djs_all_num % "2") {
tests a static number( the number of entries in your table), so you'll always execute the if or the else part.
Also, you loop on a djsall variable:
while($djs_all = mysql_fetch_array( $djs_all_db )) {
But you try to access fields from a $djs one:
<img src=\"images/about/" . $djs['username']

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.

while looping w3-quarter in w3-row-padding

Here is the problem that while looping the php in while loop in w3-row-padding of w3 responsive layout . The layout breaks
Here is the source code
<?php
$r=0;
while($r<ceil($fetch_row_count/4))
{ ?>
<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">
<?php
while($row=mysqli_fetch_array($res))
{
?>
<div class="w3-quarter">
<img src="admin/uploads/<?php echo $row['image']; ?>" alt="noodles" style="width:50%">
<h3><?php echo $row['title']; ?></h3>
<p><?php echo $row['description']; ?> </p>
</div>
<?php
}
$r++;
}
?>
</div>
Thanks for reply and comments in advance
That bottom div was not being added for each of your padded containers.
The way the code is written you are adding a padded container and then adding your w3-quarter div for each of your result sets and then repeating that a bunch of times with what looks like to me the same set of data in each one.
What you are probably trying to accomplish is just making one padded div and then echo out your result set with the w3-quarter divs inside of it.
Here is your original way with the bottom div corrected:
<?php
$r=0;
while($r<ceil($fetch_row_count/4)) {
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
}
$r++;
echo
'</div>';
}
?>
Here is the way I think you are trying to do it: (Just guessing)
<?php
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
$r = 0;
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
$r++;
//I would not actually try to control how many results you add like this.
//I would get the results from a better formatted query.
if($r < ceil($fetch_row_count/4)){
break;
}
}
echo
'</div>';
?>

add CCS style which depends on php IF statement inside mysqli_fetch_array

I want to add CCS style which depends on php IF statement. I have found something: changing the style inside "if" statement, but somehow it doesn`t work, maybe because of WHILE statement in my code.
<?php
$possible_lang_list = mysqli_query($con,
'SELECT * FROM page WHERE title = "lang" ORDER BY name1 ASC');
while ($row = mysqli_fetch_array($possible_lang_list)) {
echo '
<div class="language" id="' . $row['name2'] . '">
<p>' . $row['name1'] . '</p>
</div>
';
}
?>
I want to display only div class="language" where $row['name2'] == $x, with possibility to display whole class "language" with JavaScript. Could anyone help?
I have tried:
while ($row = mysqli_fetch_array($possible_lang_list)) {
if ($row['name2'] == $x) {
?>
<div style="display:block;">
<?php } else {
?>
<div style="display:none;">
<?php
echo '
<div class="" id="' . $row['name2'] . '">
<p>' . $row['name1'] . '</p>
</div>
</div>';
}
}
now it is working:
while ($row = mysqli_fetch_array($possible_lang_list)) {
if ($row['name2'] == $x) {
?>
<div style="display:block;">
<?php } else {
?>
<div style="display:none;">
<?php }
echo '
<div class="" id="' . $row['name2'] . '">
<p>' . $row['name1'] . '</p>
</div>
</div>';
}
Try this code :
while ($row = mysqli_fetch_array($possible_lang_list))
{
if($row['name2'] == $x)
{
echo '<div class="language" id="' . $row['name2'] . '"><p>' . $row['name1'] . '</p>
</div>';
}
else
{
echo '<div class="" id="' . $row['name2'] . '"><p>' . $row['name1'] . '</p>
</div>';
}
}
Hope it will work
Assign a style element with a true/false ternary, like below. Then just output it!
<?php
while ($row = mysqli_fetch_array($possible_lang_list)) {
$class = ($row['name2'] == $x) ? 'language' : null;
echo '
<div class="'.$class.'" id="' . $row['name2'] . '">
<p>' . $row['name1'] . '</p>
</div>
';
}
And then:

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>

Div not looping correctly

I'm trying to get my code to display multiple divs. But seems to only display one div correctly.
<?php
// database connection
//$result = mysqli_query($con,"SELECT * FROM test WHERE field5='Cohen'");
echo " <div id='1'>";
while($row = mysqli_fetch_array($result))
{
echo "<div class='tutorName'>" . $row['field1'] . "</div>";
echo "<div class='tutorPrice'>" . $row['field2'] . "</div>";
echo "<div class='tutorInstitution'>" . $row['field3'] . "</div>";
echo "<div class='tutorLocale'>" . $row['field4'] . "</div>";
echo "<div class='tutorPhone'>" . $row['field5'] . "</div>";
}
echo "</div>";
mysqli_close($con);
?>
I want the following to loop and over using the fields above (field1, feild2, etc):
<div id='' name='' class="column threecol">
<div class="course-preview premium-course">
<div class="course-image">
<img src="img.png" />
<div class="course-price">
<div class="corner-wrap">
<div class="corner"></div>
<div class="corner-background"></div>
</div>
<div class="price-text"><span class="amount">PerHour</span></div>
</div>
</div>
<div class="course-meta">
<header class="course-header">
<h5 class="nomargin">TutorName </h5>
<div class='gender'>Gender: </div>
<div class='price-range'>Price Range: </div>
<div class='institute'> Institute: </div>
</header>
</div>
</div>
</div>
Is your $result line really commented out in the real code? Or did you just add that comment for the example? If it's commented-out in the code you're testing, then that's probably the issue.
you want this? , 1 div wrap all content like table tr
$i=1;
while($row = mysqli_fetch_array($result))
{
echo " <div id='{$i}'>";
echo "<div class='tutorName'>" . $row['field1'] . "</div>";
echo "<div class='tutorPrice'>" . $row['field2'] . "</div>";
echo "<div class='tutorInstitution'>" . $row['field3'] . "</div>";
echo "<div class='tutorLocale'>" . $row['field4'] . "</div>";
echo "<div class='tutorPhone'>" . $row['field5'] . "</div>";
echo "</div>";
$i++;
}

Categories