my code is not displaying the first result in the while loop.
my query is working right on phpmyadmin but somethings going wrong on index page.
I have the following code:
<?php
$test=mysql_query("SELECT * FROM newsites");
$deneme=mysql_fetch_row($test);
?>
<div class='container'>
<div class='row'>
<?php
while ($deneme=mysql_fetch_assoc($test)) {
extract($deneme);
echo '<div class="col-md-3 col-sm-6 col-xs-12 back-colour">';
echo '<td><img class="img-responsive" src="images/'.$deneme['site_pic'].'" width="120" height="20"/></td>';
echo '<p class="box-design">'.$deneme['site_name'].'</p>';
echo '<p class="box-design">'.$deneme['site_link'].'</p>';
echo '<p class="box-design">'.$deneme['site_ref'].'</p>';
echo '<p class="box-design">'.$deneme['site_type'].'</p>';
echo '</div>';
}
?>
</div>
</div>
It looks like the first row is already taken from the resultset outside the while loop.
On line 3 you have $deneme=mysql_fetch_row($test);
I'd say remove it and you're good to go.
Related
From the image below, you can see there is a empty space in second row (red rectangular), by right it should filling up the empty space. This is happening because the first image title is longer than others.
I know by using HTML code, I can use clear: both; and put under the first row to solve it. But my info is read from database. Following is my php and mysql code, I have simplified it, please ignore the method I use to display the image.
$selectSQL="SELECT *, DATE_FORMAT(published_date,'%d %M %Y') AS eventDate FROM media";
while($rows=mysql_fetch_array($selectSQL)){
<div style="float:left">
<img src="load_image.php?id=<?php echo $rows["id"]; ?>" width="150px" height="212px">
<div><?php echo $rows["title"]; ?></div>]
<div><?php echo $rows["eventDate"]; ?></div>
</div>
}
I have one solution, which is compare the number of element = 5. Something like the following code. But I'm not sure how to check it the number of element. Please advice or please suggest me if you have another solutions. Thanks!
if($num_element==5){
echo '<div class="clear"></div>';
}
$ i = 0;
while($rows=mysql_fetch_array($selectSQL)){
<div style="float:left">
<img src="load_image.php?id=<?php echo $rows["id"]; ?>" width="150px" height="212px">
<div><?php echo $rows["title"]; ?></div>]
<div><?php echo $rows["eventDate"]; ?></div>
</div>
$i++;
if($i % 5 == 0){
echo '<div class="clear"></div>';
}
}
you can try this code to check the number of items totally displayed and after 5 elements it will print a clear div
You can use substr() to concat your title when it is very long. you can try it like this:
while($rows=mysql_fetch_array($selectSQL)){
<div style="float:left">
<img src="load_image.php?id=<?php echo $rows["id"]; ?>" width="150px" height="212px">
<div title="<?php echo $rows["title"];?>">
<?php if(strlen($rows["title"]) > 6){
echo substr($rows["title"],0, 6).'...';
}else{
echo $rows["title"];
} ?>
</div>
<div><?php echo $rows["eventDate"]; ?></div>
</div>
}
With this code you can customize the length of the words in your title.
I would like to create a new containing <div> after 3 results, using PDO result loop.
For my self-study-project I have to made a product page with bootstrap and after every 3rd record I have to make a new row and show again 3 col-md-4's, etc, etc.
Now I have this as my code:
<div class="row">
<?php
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
?>
<div class="col-md-4">
<div class="product">
<div class="title"><?php echo $row->pname ?></div>
<div class="img"><img
src="../product/img/<?php echo $row->pnumber ?>/<?php echo $row->pthumbnail ?>.jpg?$pop210x210$"/>
</div>
<div class="vijftien"></div>
<div class="deliver">Levertijd: <strong><?php echo $row->pdelivertime ?></strong></div>
<div class="vijf"></div>
<div class="other"></div>
<div class="row">
<div class="col-md-6">
<div class="price"><?php echo $row->pprice ?></div>
</div>
<div class="col-md-6">
<div class="order">
<button class="log_in" id="doLogin">Meer informatie</button>
</div>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
I have visited and studied other questions but I do not really get the idea of how they are doing it and how I can implement the correct method into my code.
As tadman stated in the comment under your question. The best approach should use a modulus operator (%) with 3.
Place your separating condition at the start of each iteration. (Demo)
Like this:
$x=0; // I prefer to increment starting from zero.
// This way I can use the same method inside a foreach loop on
// zero-indexed arrays, leveraging the keys, and omit the `++` line.
echo "<div class=\"row\">";
foreach($rows as $row){
if($x!=0 && $x%3==0){ // if not first iteration and iteration divided by 3 has no remainder...
echo "</div>\n<div class='row'>";
}
echo "<div>$row</div>";
++$x;
}
echo "</div>";
This will create:
<div class="row"><div>one</div><div>two</div><div>three</div></div>
<div class='row'><div>four</div><div>five</div><div>six</div></div>
Late Edit, here are a couple of other methods for similar situations which will provide the same result:
foreach(array_chunk($rows,3) as $a){
echo "<div class=\"row\"><div>",implode('</div><div>',$a),"</div></div>\n";
}
or
foreach ($rows as $i=>$v){
if($i%3==0){
if($i!=0){
echo "</div>\n";
}
echo "<div class=\"row\">";
}
echo "<div>$v</div>";
}
echo "</div>";
To clarify what NOT to do...
Sinan Ulker's answer will lead to an unwanted result depending on the size of your result array.
Here is a generalized example to expose the issue:
Using this input array to represent your pdo results:
$rows=["one","two","three","four","five","six"];
Sinan's condition at the end of each iteration:
$i=1;
echo "<div class=\"row\">";
foreach($rows as $row){
echo "<div>$row</div>";
if($i%3==0)echo "</div>\n<div class='row'>"; // 6%3==0 and that's not good here
// 6%3==0 and will echo the close/open line after the content to create an empty, unwanted dom element
$i++;
}
echo "</div>\n\n";
Will create this:
<div class="row"><div>one</div><div>two</div><div>three</div></div>
<div class='row'><div>four</div><div>five</div><div>six</div></div>
<div class='row'></div> //<--- this extra element is not good
You need to add a new closure tag and open new one every 3th iteration.
<div class="row">
<?php
$sql = "SELECT * FROM products";
$stmt = $conn->query($sql);
$stmt->execute();
$i=1;
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
?>
<div class="col-md-4">
<div class="product">
<div class="title"><?php echo $row->pname ?></div>
<div class="img"><img
src="../product/img/<?php echo $row->pnumber ?>/<?php echo $row->pthumbnail ?>.jpg?$pop210x210$"/>
</div>
<div class="vijftien"></div>
<div class="deliver">Levertijd: <strong><?php echo $row->pdelivertime ?></strong>
</div>
<div class="vijf"></div>
<div class="other"></div>
<div class="row">
<div class="col-md-6">
<div class="price"><?php echo $row->pprice ?></div>
</div>
<div class="col-md-6">
<div class="order">
<button class="log_in" id="doLogin">Meer informatie</button>
</div>
</div>
</div>
</div>
</div>
<?php
if($i%3==0)echo "</div><div class='row'>";
$i++;
} ?>
$x = 0;
echo "";
foreach($rows as $row)
{
/* added one more condition for removing empty div.... */
if ($x != 0 && $x % 3 == 0 && $x < count($rows))
{
echo "</div>\n<div class='row'>";
}
echo "<div>$row</div>";
++$x;
}
echo "";
I'm trying to configure my database data that is being pulled with php to look like my homepage.
I can't figure out what I need to do.
This is for a project for school, and I have very limited knowledge on php.
Here is my php script.
<?php
include('mysql_connect.php');
$dbh = con();
foreach($dbh->query("SELECT * FROM `Product` WHERE `ProductType` = 'memory' ",PDO::FETCH_ASSOC) as $row){
$Pname = $row['ProductName'];
$description = $row['description'];
$price = $row['ProductPrice'];
$ID = $row['ProductID'];
echo <div class="row">;
echo <div class="col-sm-4 col-lg-4 col-md-4">;
echo <div class="thumbnail">;
<img src="/var/www/html/Pictures/gigb75m_home.jpg" alt="">
echo "<div class="caption">";
echo "<h4 class="pull-right">$price</h4>";
echo "<h4>$Pname</h4>";
echo "<p>$description</p>";
echo "<p>$ID</p>";
}
?>
</div>
</div>
</div>
Here is my index that has my desired results.
<div class="row">
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<img src="/var/www/html/Pictures/gigb75m_home.jpg" alt="">
<div class="caption">
<h4 class="pull-right">$74.99</h4>
<h4>GIGABYTE GA-B75M
</h4>
<p>Micro ATX, LGA 1155, Intel B75 Express Chipset, 2200MHz DDR3 (O.C.), SATA III (6Gb/s), 7.1-CH Audio, Gigabit LAN, USB 3.0 - GA-B75M-D3H</p>
</div>
</div>
</div>
Picture of homepage: http://imgur.com/g72Wrxk
Any help/guidance is greatly appreciated, this is driving me crazy.
Use '.' for string concatinations.
For example:
echo '<h4 class="pull-right">' . $price . '</h4>';
You have to use single quote to echo content in your PHP script like this for instead :
echo '<div class="row">';
The single quotes are used to explain that it's a string to show. You have to do the same for your other echo part.
Or you can escape the HTML part from the PHP and replace by vars inside it like this :
<?php
include('mysql_connect.php');
$dbh = con();
echo '<div class="row">';
foreach($dbh->query("SELECT * FROM `Product` WHERE `ProductType` = 'memory' ",PDO::FETCH_ASSOC) as $row){
$Pname = $row['ProductName'];
$description = $row['description'];
$price = $row['ProductPrice'];
$ID = $row['ProductID'];
?>
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<img src="/var/www/html/Pictures/gigb75m_home.jpg" alt="">
<div class="caption">
<h4 class="pull-right"><?php echo $price; ?></h4>
<h4><?php echo $Pname; ?></h4>
<p>$description</p>
<p>$ID</p>
</div>
</div>
</div>
<?php
}
?>
</div>
Also you have to be careful about the row system of bootstrap to echo col-md-4 inside a row, you can add 3 items like this per row. So you have to close and reopen a row each time you fetch 3 elements to get a correct HTML structure.
You didn't supplied any information about your database, yet the first problem that's visible is your echo's
To output HTML through PHP you have to output pure strings.
An example for that would be ""
In PHP when you want to connect different kinds of inputs like variables and "" or '' etc you connect them with a dot.
For example:
$string = ' hello3';
echo "Hello 1 ".' hello2 '.$string;
The outcome will be:
Hello 1 hello2 hello3
I hope you got it the hang of it.
I have been trying to put HTML inside PHP for a while, but I could never get it to work. Instead I have had to use this
<?php //code in here
while($record = mysql_fetch_array($myData)){
?>
//HTML code
<?php
}
?>
and it has worked for a while, but now I'm implementing a search function to the page, and I need all HTML to be dynamically generated, or the page will be displayed twice. Therefore, i'm in need of some help. This is the old-new code I have at the moment.
<?php
$sql = "SELECT * FROM `LISTINGS` ORDER BY YA DESC $limit";
$myData = mysql_query($sql,$con);
while($record = mysql_fetch_array($myData)){
//this is what i have manage to get done
echo '<div class="cards-row">';
echo '<div class="card-row">';
echo '<div class="card-row-inner">';
echo '<div class="card-row-image" data-background-image="http://domains/$record["IMAGENAME"].jpg">';
echo '</div>';
echo '<div class="card-row-body">';
echo '<h2 class="card-row-title">';
echo '<a href="http://domain/listing-detail.php?ID='; $record['ID']echo'">'
echo '</a>';
echo '</h2>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
?>
Static/dynamic code
<div class="card-row">
<div class="card-row-inner">
<div class="card-row-image" data-background-image="http://www.domain/<?php echo $record['IMAGENAME'];?>.jpg" width="150" height="180" ">
</div><!-- /.card-row-image -->
<div class="card-row-body">
<h2 class="card-row-title">
<a href="http://www.domain/listing-detail.php?ID=<?php echo $record['ID'];?>"> <?php custom_echo ($record['TITLE'], 80); ?><?php if($record['STREET']===' '){ echo "Not provided" ;}?>
</a>
</h2>
<div class="card-row-content"><?php custom_echo ($record['DESCRIPTION'], 250); ?><?php if($record['STREET']===' '){ echo "Not provided" ;}?></div><!-- /.card-row-content -->
</div><!-- /.card-row-body -->
<div class="card-row-properties">
<dl>
<dd></dd><dt>Visit Website</dt>
<dd></dd><dt>More Info</dt>
<dd>Added</dd><dt><?php echo $record['YA'];?>/<?php echo $record['MU'];?>/<?php echo $record['DU'];?></dt>
<dd>Viewed</dd><dt>Visited</dt>
</dl>
</div><!-- /.card-row-properties -->
</div><!-- /.card-row-inner -->
</div><!-- /.card-row -->
</div><!-- /.cards-row -->
<br/>
<?php
}
?>
My goal is to put all the code above inside
while($record = mysql_fetch_array($myData)){
Although this is not the best way when it comes to readability and maintainability, the way I usually process HTML with PHP is by echoing it.
echo "<div style=\"color:red\">test</div>";
If I need to add PHP content then
$myVar = "test";
echo "<div style=\"color:red\">" . $myVar . "</div>";
And if I already have some HTML code I'd like to escape, I can easily do so using Notepad++'s replace all function by replacing " with \"
You have an error in line 15
echo '<a href="http://domain/listing-detail.php?ID='; $record['ID']echo'">'
Change it to:
echo '<a href="http://domain/listing-detail.php?ID='.$record['ID'].'">';
Also, are you saving it in ".php" extension?.
After the last echo, you forget a '}'.
Finally, remove your mysql_ function and use mysqli_ or PDO, if you're on PHP7.
I have a question that bothers me for a long time. Lets say i have a code with several nested div's and span's. All these compose a square with an image inside.
echo '<div> <div> <div> <div> <span> <img src='.$image.'> </span></div></div></div>';
Only that this code has about 15 rows.
From what i know when echo-ing the results from db in that form i put in the loop the whole html code. It looks clumsy this way.
It is there a better practice ?
foreach ($query->result() as $row)
{
$row->address_link=strtolower($row->network);
echo '<li class="col-md-3 isotope-item '.$row->network.'">';
echo '<div class="portfolio-item img-thumbnail">';
echo '<table border="0"><tr>';
echo '<a href="order/'.$row->address_link.'/'.$row->value.'" class="thumb-info">';
echo '<img alt="" class="img-responsive" src="img/'.$row->address_link.'.png">';
echo '<span class="thumb-info-title">';
echo '<span class="thumb-info-inner">'.$row->value.' Euro</span>';
echo '</span>';
echo '<span class="thumb-info-action">';
echo '<span title="Universal" href="order/'.$row->address_link.'/'.$row->value.'" class="thumb-info-action-icon"><i class="icon icon-link"></i></span>';
echo '</span>';
echo '</a>';
echo '</div>';
echo '</tr><tr>';
echo '<span class="thumb-info-type">'.$row->value*$row->rate.' Eur</span>';
echo '</tr></table>';
echo '</li>';
}
If you are new to php you can define a function for this:
function wrapImage($src){
return '<div> <div> <div> <div> <span> <img src='.$src.'> </span></div></div></div>';
}
And just use echo wrapImage($src) where you need it with different params.
EDIT: consider following way of presenting the data:
<?php
$query = 'select * from Unicorns';
foreach ($query->result() as $row){
$row->address_link=strtolower($row->network);
?>
<!-- html -->
<li class="col-md-3 isotope-item <?php echo $row->network; ?>">
<div class="portfolio-item img-thumbnail">
<table border="0"><tr>
<a href="order/<?php echo $row->address_link.'/'.$row->value; ?>" class="thumb-info">
<img alt="" class="img-responsive" src="img/'.$row->address_link.'.png">
<span class="thumb-info-title">
<span class="thumb-info-inner"><?php echo $row->value; ?> Euro</span>
</span>
<span class="thumb-info-action">
<span title="Universal" href="order/<?php echo $row->address_link.'/'.$row->value ?>" class="thumb-info-action-icon"><i class="icon icon-link"></i></span>
</span>
</a>
</div>
</tr><tr>
<span class="thumb-info-type"><?php echo ($row->value*$row->rate); ?> Eur</span>
</tr></table>
</li>
<!-- /html -->
<?php } ?>
It is called spaghetti code .. and it is NOT the best practice but is better then your example in case the HTML is more then the PHP data.
first of all, dont use echo in loops (optimalization), store your output in variable and print it only once.
Repeated code can be stored inside function
function square($image){
return '<div> <div> <div> <div> <span> <img src='.$image.'> </span></div></div></div>';
}
$output = '';
while ($loop){
$output .= square($image);
}
echo $output