I have a question about my code. When i try to fill my html with my fetched array i only get blank spaces instead of the actual results. Can anyone tell me why ?
<?php
require_once("php/loader.inc.php");
include("php/header.php");
include("php/userpanel.php");
$gebruiker = $_SESSION['user'];
?>
<div class="large-8 columns">
<div class="row">
<?php
$query = "
SELECT
items.naam,
items.prijs,
items.image_url
FROM inventory
JOIN items
ON items.id = inventory.item_id
WHERE
gebruiker_id = ?
";
$stmt = $db->prepare($query);
$stmt-> bind_param('i', $gebruiker->id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc())
{
$user_items[] = $row;
echo '<div class="large-3 small-4 columns">
<img src="'. $user_items['image_url'] .'">
<div class="panel">
<h6>'. $user_items['naam'] .'</h6>
<h6 class="subheader">'. $user_items['prijs'] .'</h6>
</div>
</div>';
}
?>
By the way, when i try print_r($user_items); it gives me the expected result.
Thanks in advance!
I think you want to change this:
$user_items[] = $row;
to this:
$user_items = $row;
Otherwise your indexes are 1 dimension 'deeper'
As an example:
$row = array("image_url" => "xy");
after this line:
$user_items[] = $row;
You can't access it with:
echo $user_items["image_url"]; //Wrong
you would have to do this:
echo $user_items[0]["image_url"]; //works
You don't need this line $user_items[] = $row; you can use $row directly
while ($row = $result->fetch_assoc())
{
echo '<div class="large-3 small-4 columns">
<img src="'. $row['image_url'] .'">
<div class="panel">
<h6>'. $row['naam'] .'</h6>
<h6 class="subheader">'. $row['prijs'] .'</h6>
</div>
</div>';
}
Related
I already get all data but I don't know how to fetch data with switch column for ex 1st data in left img in right , 2 nd data in right img in left( display will be in like my html )
Here is My php
$SQL = "SELECT * FROM DB_NEWS";
$result = mysql_query($SQL);
while ($row = mysql_fetch_array($result)){
$data = $row["DATA"];
$img = $row["IMG"];
Here is my HTML ( I want data to fetch like this )
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/ >
<div class="col-md-6">
<div style="height:150px;background-color: red"> $data</div>
</div>
<div class="col-md-6">
<div style="height:150px;background-color: blue">$img</div>
</div>
<div class="col-md-6">
<div style="height:150px;background-color: blue"> $img</div>
</div>
<div class="col-md-6">
<div style="height:150px;background-color: red">$data</div>
</div>
Consider the four div as a combination of 2 div. Use a counter with mod operator to match first two div or 2nd two div you are dealing with.
$i = 0 ;
while ($row = mysql_fetch_array($result)){
if( $i++ % 2 == 0 ) {
$col1 = $row["DATA"];
$col2 = $row["IMG"];
$color1 = 'blue' ;
$color2 = 'red' ;
}
else {
$col1 = $row["IMG"];
$col2 = $row["IMG"];
$color1 = 'red' ;
$color2 = 'blue' ;
}
?>
<div class="col-md-6">
<div style="height:150px;background-color: <?php echo $color1;?>"><?php $col1;?></div>
</div>
<div class="col-md-6">
<div style="height:150px;background-color: <?php echo $color2;?>"><?php $col2;?></div>
</div>
<?
php
} //end of while
?>
Logic:
It will print two div at a time. when the loop begin $i is zero. 0 %2 will result in 0; so first data will be printed. after that $++ will will make it 1. when the next time loop comes 1%2 is != 0 so else part will be considerd.
Have you reviewed PHP syntax?
Rather than saving your file as .html, save it as .php. You can write HTML exactly the same, but have the added benefit of being able to use PHP as well. Then you can do this:
<?php
// You can start the file in PHP
$SQL = "SELECT * FROM DB_NEWS";
$result = mysql_query($SQL);
while ($row = mysql_fetch_array($result)){
$data = $row["DATA"];
$img = $row["IMG"];
?>
<!-- Now we are in HTML, but still hop back into PHP to work with variables. -->
<?foreach ($row as mysql_fetch_array( $result ) ):?>
<? $data = $row['DATA'];
$img = $row['IMG'];
?>
<div>
<div> <? echo $data ?> </div>
<div> <? echo $img ?> </div>
</div>
<? endforeach ?>
If I was to write this, this is how I would do it.
Updated code to reflect your OP markup.
<?php
$SQL = "SELECT * FROM DB_NEWS";
$result = mysql_query($SQL);
// define variable
$html = NULL;
while ($row = mysql_fetch_array($result)) {
$html .= ''
. '<div class="col-md-6">'
. '<div style="height:150px;background-color: red">' . $row['DATA'] . '</div>'
. '</div>'
. '<div class="col-md-6">'
. '<div style="height:150px;background-color: blue">' . $row['IMG'] . '</div>'
. '</div>';
}
echo $html;
thanks for taking a look at my question.
I'm trying to get PHP to output bootstrap rows that only contain 2 col-md-6 columns, instead of outputting 1 row with 1 col for each iteration.
Searching here on Stackoverflow I found a solution that makes sense but when I implement it, the HTML that I get makes no sense!
I should be getting this:
<div class="row">
<div class="col-md-6"></div>
<div class="col-md-6"></div>
</div>
<div class="row">
<div class="col-md-6"></div>
<div class="col-md-6"></div>
</div>
...But I'm getting this:
<div class="col-md-6">
<div class="row">
<div class="col-md-6"></div>
<div class="col-md-6"></div>
</div>
<div class="row">
<div class="col-md-6"></div>
<div class="col-md-6"></div>
</div>
</div>
CODE:
<?php
require_once('somedb.php');
$query = mysqli_query($conn, "SELECT * FROM notyourbusiness");
$rowCount = mysqli_num_rows($query);
$i = 0;
echo '<div class="row">';
if($rowCount > 0){
while($row = mysqli_fetch_assoc($query)){
?>
<?php echo '<div class="col-md-6">'; ?>
<img src="img/project/<?php echo $row['thumb'] ?>" class="work-thumbnail" width="100">
<h2><?php echo $row["name"]; ?></h2>
<?php echo '</div>'; ?>
<?php
$i++;
if ($i%2 == 0) echo '</div><div class="row">';
} ?>
<?php } ?>
</div>
Any help will be greatly appreciated, thanks!
Can you provide us more information? It seems that the first is comming from outside of this php code.
You can try this
<?php
$query = mysqli_query($conn, "SELECT * FROM notyourbusiness");
$rowCount = mysqli_num_rows($query);
$i = 0;
if($rowCount > 0){
while($row = mysqli_fetch_assoc($query)){
$row_draw = ($i % 2 == 0) ? true : false;
# Start row div
if ($row_draw)
{
print "<div class='row'>";
}
# Print Column
print "<div class='col-md-6'>";
?>
<img src="img/project/<?php echo $row['thumb'] ?>" class="work-thumbnail" width="100">
<a href="javascript:void(0);">
<h2><?php echo $row["name"]; ?></h2>
</a>
<?php
print "</div>";
# End row div
if ($row_draw)
{
print "</div>";
}
$i++;
}
}
?>
The search results should display like this
But my results are stacking on top of each.
Here is my code :
<div class="container">
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("thesis") or die(mysql_error());
$search = trim( $_POST['SearchKeywords']);
$query = " SELECT * FROM new_data WHERE Product_Title or Product_link LIKE'%$search%' ";
$sql = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($sql);
$count == 0;
if ($count == 0) {
echo "Sorry, Nothing Found !!";
}else{
while ($row = mysql_fetch_array($sql))
{
$img = $row ['Product_Img'];
$link = $row ['Product_link'];
$title = $row ['Product_Title'];
$price = $row ['Product_Price'];
?>
<div class="card">
<div class="front alert alert-success">
<?php echo "$title";
echo "<img src='$img' width='80px' height='100px'>";
echo "$price"; ?>
</div>
</div>
<?php
};
};
?>
</div> <!-- Container -->
Those div blocks are inside a container.
I added a bootstrap class in order for better a design.
You can use thumbnails with custom content
<div class="row">
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="..." alt="...">
<div class="caption">
<h3>Thumbnail label</h3>
<p>...</p>
<p>Button Button</p>
</div>
</div>
</div>
</div>
I used a counter inside while loop.
Which will check, when there are already 4 blocks/ products in a single row then it will create a new row
<?php
if($productCount == 0)
{
echo "<div class='row'>"; }
}
$productCount++;
if($productCount==4)
{
echo "</div>" ;
$productCount = 0;
}
?>
My php slider is sort of working i have managed to get it to link to my database. However, i need it to loop through all the images within my slider and my code isn't work but i believe i have missed a loop query?!
THIS IS MY CODE WHERE MY SLIDER IS:
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
<img src="<?php print $row['image']?>"/>
<!-- <img src="images/nivo/arts.png" alt="the grand theatre and nothern ballet">
<img src="images/nivo/slider3.png" alt="leeds night light slider image">
<img src="images/nivo/slider2.png" alt="Leeds Trinity slider image">
<img src="images/nivo/slider4.png" alt="leeds art hotels">
<img src="images/nivo/slider1.png" alt="leeds art slider image"> -->
</div>
</div>
THIS IS MY CODE TO RUN MY SLIDER AT THE MOMENT:
<?php
$myQuery = "SELECT * FROM SliderImg";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));
$row = mysqli_fetch_array($result);
?>
you need a loop in your php:
<?php
$myQuery = "SELECT * FROM SliderImg";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));
$rows = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$rows[] = array(
'image' => $row['image'];
)
}
?>
And in your HTML:
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
<?php
foreach($rows as $row) { ?>
<img src="<?php print $row['image']?>"/>
<?php
} ?>
</div>
</div>
<?php
$myQuery = "SELECT * FROM SliderImg";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));
?>
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
// use a while here
<?php while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){ ?>
<img src="<?php print $row['image']?>"/>
<?php } ?>
</div>
</div>
you can use foreach to loop through your data, like this
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
<?php foreach ($row as $key => $item): ?>
<img src="<?php echo $item['image']?>"/>
<?php endforeach; ?>
</div>
</div>
hope it help..
$resultSet = mysql_query("SELECT * FROM news ORDER BY id DESC LIMIT 7");
$product_count = mysql_num_rows($resultSet);
if($product_count >0){
while( mysql_fetch_array($resultSet)){
$id= $row["id"];
$cat= $row["categories"];
$title= $row["title"];
$image= $row["image"];
$txt= $row["txt"];
$res= $row["resource"];
}
}
else
{ echo'site is under maintaince ';}
echo " <div class=\"row-fluid\">
<div class=\"span6 post no-margin-left\">
<figure>
<img src=\"$image\" alt=\"Thumbnail 1\" />
<div class=\"cat-name\">
<span class=\"base\">'$cat'</span>
<span class=\"arrow\"></span>
</div>
</figure>
<div class=\"text\">
<h2>'$title'</h2>
<p>$txt</p>
<div class=\"meta\">By ' $res' | '$date' | 15 comments</div>
</div>
</div>
</div>";
?>
The code is not showing the output results of the table. Can you tell me what I am doing wrong? I want to show the output in the div class. My paging is working correctly, but it is not showing the output correctly. Can you guide me? I will really be grateful.
while( mysql_fetch_array($resultSet)){
$id= $row["id"];
$cat= $row["categories"];
$title= $row["title"];
$image= $row["image"];
$txt= $row["txt"];
$res= $row["resource"];
}
}
Where is $row in the lopp? However, to populate all the results, you need the printing inside the loop, otherwise you won't get them iterated