I am trying to insert a <br clear="all" /> after each fifth div. Below is how I create the divs in php and then I try to use jquery to seperate them. But it is not working! Any help would be greatly appreciated! Thanks in advance!
PHP:
$get = mysql_query("SELECT * FROM Products $cate ") or die(mysql_error());
while($row= mysql_fetch_array($get)){
$pname = $row['Pname'];
$image = $row['Pimage'];
$id = $row['ID'];
?>
<div class="productCat"><img src="../products/<?php echo $image ?>" width="100" height="100" /><br /><?php echo $pname ?></div>
<?php
}
?>
JQUERY:
$(".productCat :nth-child(5)").append("<br clear='all'/>");
There isn't supposed to be a space after .productCat.
Also, why not just do it in the PHP? Just add a counter:
$count=0;
while($row= mysql_fetch_array($get)){
$count++;
$pname = $row['Pname'];
$image = $row['Pimage'];
$id = $row['ID'];
?>
<div class="productCat"><img src="../products/<?php echo $image ?>" width="100" height="100" /><br /><?php echo $pname ?></div>
<?php
if($count%5==0)
echo "<br clear='all'/>";
}
?>
put the space out between class selector and nth-child :
$(".productCat:nth-child(5)").append("");
The problem is that it's looking for the multiple-of-five-th children of .productCat, because of the extra space. EDIT: Actually, it's only looking for the 5th child of .productCat, because you left the n out.
Besides, what's wrong with:
/* CSS: */
.productCat:nth-child(5n+1) {clear: left;}
This CSS-only solution is far more efficient.
Why not doing it in PHP ? If you use jQuery, people without javascript won't have your "br" ...
<?php
$get = mysql_query('SELECT * FROM Products '.$cate) or die(mysql_error());
$i=0;
while($row = mysql_fetch_array($get)){
$i++:
?>
<div class="productCat"><img src="../products/<?php echo $row['Pimage']; ?>" width="100" height="100" /><br /><?php echo $row['Pname']; ?></div>
<?php
if($i == 5){
echo '<br clear="all" />';
$i=0;
}
}
?>
Related
When I run the following file I get the database data i.e it prints it out on the website so I know my connections are good.
<html>
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo $row["name"], $row["image"];
}
?>
</div>
</html>
However when I try and format the results like below
<html>
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo <div id = "bookbar">
<img src= "$row['image']" alt = "image">
<p> $row['name'] </p>
</div>
}
?>
</div>
</html>
it doesn't work. Can anyone help me fix the code?
Maybe try this your code didn't close/open the php tags properly also don't echo like that
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<div id = "bookbar">
<img src= "<?php echo $row['image'] ?>" alt = "image">
<p><?php echo $row['name']; ?></p>
</div>
<?php
}
}
$conn->close();
?>
If you want to echo something out
Its better to close on open the php tags like so
PHP goes here
?>
HTML goes here
<?php
PHP goes here
And if you want to echo something inside the HTML just do this
<span> <?php echo "something" ?> </span>
much easier and makes the code easier to read.
change your echo statement to -
echo '<div id = "bookbar"><img src= "' . $row['image'] . '" alt = "image"><p>'. $row['name'] .'</p>'
Your issue is a syntax problem - you can't use echo like that, it has to echo a string variable. You should be seeing an error message about it.
You could keep the echo statement and put all the HTML inside a string, and concatenate (or interpolate) the PHP data into it. But IMO the easiest thing here in terms of readability and maintenance is to step out of the PHP tags, print the HTML, embed some PHP tags in it for the variables, and then step back in again to continue with the code. It makes the HTML far easier to understand:
?>
<div id="bookbar">
<img src="<?php echo $row['image'] ?>" alt="image">
<p><?php echo $row['name'] ?></p>
</div>
<?php
When you are in php mode you should echo strings as php variables wrapped with single quotes:
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo '<div id = "bookbar">';
echo '<img src="' . $row['image'] . '" alt = "image">';
echo '<p>' . $row['name'] . '</p>';
echo '</div>';
}
Below is my code for display information of an image including its file, but this is not displaying, I don't know why.
<?php
include('config/dbconnect.php');
if(isset($_GET['id']))
{
$id = $_GET['id'];
$sql = mysqli_query($con, "SELECT * FROM collage WHERE id = '$id'");
while($row = mysqli_fetch_array($sql)) {
$name = $row['name'];
$content = $row['content'];
$size = $row['size'];
$type = $row['type'];
$date_upload = $row['date_upload'];
$file = $row['file'];
}
}
?>
<div class='body-content'>
<div class='img-name-cont'><h3><?php echo $name; ?></h3></div>
<div class='img-detail-cont'>upload: <?php echo $date_upload; ?><br>type: <?php echo $type; ?><br>size: <?php echo $size; ?>KB </div>
<div class='img-file-cont'>
<img src="img/collage/<?php echo $row['file'] ?>" style="width:130px; height:100%"></div>
<div class='img-content-cont'><?php echo $content; ?></div>
</div>
First of all use inspect to check whether the image is actually pointed to by the code. that is the actual loation might not be pointed at. what is your folder structure.
" style="width:130px; height:100%">
your code is ok. as long as php echo $row['file'] returns a file
The problem here is "Your all the variables are local to the while loop". so you can't access it outside of the while loop. Try this code.
<?php
include('config/dbconnect.php');
if(isset($_GET['id']))
{
$id = $_GET['id'];
$sql = mysqli_query($con, "SELECT * FROM collage WHERE id = '$id'");
while($row = mysqli_fetch_array($sql)) {
?>
<div class='body-content'>
<div class='img-name-cont'><h3><?php echo $row['name']; ?></h3></div>
<div class='img-detail-cont'>upload: <?php echo $row['date_upload']; ?><br>type:
<?php echo $row['type']; ?><br>size: <?php echo $row['size']; ?>KB </div>
<div class='img-file-cont'>
<img src="img/collage/<?php echo $row['file']; ?>" style="width:130px; height:100%"></div>
<div class='img-content-cont'><?php echo $row['content']; ?></div>
</div>
<?php
}
}
?>
Be sure to check echo $row['file'] if it returns the correct string of your image path with the correct file extension .
try this one :
first of all you have to check if your image path ok or not using this php function:
<?php
$img=getimagesize($imagewithpath);
if($img=="")
{ echo "there is no image ";}
else{ ?> <img src="<?php echo $imagewithpath; ?>" style="width:130px; height:100%"> <?php } ?>
As answered by Dulaj Sanjaya all your variables are local so you can't use that outside of while.
Second if you want to use your code then you have taken all data of $row in different variables then used outside of file then why don't you used same $file for image why you have used $row['file']?
Simply replace this line as
<div class='img-file-cont'>
<img src="img/collage/<?php echo $file; ?>" style="width:130px; height:100%"></div>
This is a very basic problem but I am very new to PHP.
I need to show results in such scenario that only three records in same line then add <br /> and then on next line, same thing should happend. I am unable to make its logic and in a great trouble :(
Right now, I am just using the simple way i.e.
while($data = mysql_fetch_array($res_set)) {
?>
<div><?php echo $data['name']?><img src="images/<?php echo $data['image']?>" /></div>
<?php
}
this is my code
$cnt = 0;
while ($prd = mysql_fetch_array($res)) {
?>
<div class="imageRow">
<?php
$cat_id = $prd['cat_id'];
$sql = "select * from tbl_category where id = $cat_id";
$cat_res = mysql_query($sql);
$cat_data = mysql_fetch_array($cat_res);
$cat_name = $cat_data['name'];
?>
<div class="set">
<div class="single first">
<img src="<?php echo $ru ?>admin/product_images/<?php echo $cat_name ?>/large/<?php echo $prd['thumb_img'] ?>" style="height: 100px; width: 100px;" /><br />
Choose
</div>
</div>
</div>
<?php
$cnt = $cnt++;
if($cnt%3 == 0) {echo "<br />";}
}
//echo $cnt;
?>
any help will be appreciated.
Thanks
<div class="imageRow">
<?php
$i = 0;
while($data = mysql_fetch_array($res_set)) { ?>
<div>
<?php echo $data['name']; ?><img src="images/<?php echo $data['image']; ?>" />
</div>
<?php
$i++;
if($i % 3 == 0) {
echo '</div><div class="imageRow"><br />';
}
}
?>
</div>
Explanation:
Set the variable $i to a number, which will then be used to keep track of how many items you've written.
Then, within the while loop, increment $i ($i++), which is the same as $i = $i + 1; By doing this, you always know which item you're on - whatever the value of $i is. Some people choose to set it to 1 initially then increment at the very end - other like to set it to 0, and set it near the beginning - either is completely fine - whatever you need it to do / whichever way you like better.
Lastly, check if $i is evenly divisible by 3 (kind of like remainder - it's called "mod" and is represented by the percent symbol). If it is, then echo a line break.
Try this
<?php
echo "<div>";
while($data = mysql_fetch_attay($res_set)) {
?>
<div style="width:30%; float:left"><?php echo $data['name]?><img src="images/<?php echo $data['image']?>" /></div>
<?php
}
echo "</div>";
?>
Output
<div>
<div>1</div> <div>2</div> <div>3</div>
<div>1</div> <div>2</div> <div>3</div>
<div>1</div> <div>2</div> <div>3</div>
</div>
That's some super ugly code you have there. This will do what you need, and is cleaner.
<?php
$i=1;
while($data = mysql_fetch_attay($res_set)) {
$frame = '<div><img src="%s" /></div>';
printf($frame, $data['image']);
if($i % 3 == 0) { echo '<br />'; }
$i++;
}
Also, <img> elements are already block elements to begin with, you really don't need to wrap another <div> around them.
I wanted to display all of my shirts but I always get the same error
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /homepages/36/d362778419/htdocs/home/specials.php on line 34
Here is the code I have currently in place
<?php
require "connect2.php";
$sql = mysql_query("SELECT * FROM shirts WHERE all='1'");
$runrows = mysql_fetch_array($sql);
$title = $runrows['title'];
$picture = $runrows['picture'];
$newp = $runrows['newp'];
$date = strftime("%b %d, %Y %l:%M %p" ,strtotime($runrows['date']));
$oldp=$runrows['oldp'];
$viewl=$runrows['viewl'];
$shirtl = $runrows['shirtl'];
echo "";
?>
<div class="specialsListBoxContents centeredContent back" style="width:25%;"><div class="product-col" > <div class="img">
<a href="detail.php?id=<?php echo $id; ?>"><img src="<?php echo $picture; ?>" alt="<?php echo $title; ?>" title=" <?php echo $title; ?> " width="190"
height="160" /></a> </div> <div class="prod-info"> <div class="wrapper">
<div class="price"> <strong><span class="normalprice"><?php echo $oldp; ?
></span><br /><span class="productSpecialPrice"><?php echo $newp; ?></span></strong> </div>
<div class="button"><a href="detail.php?id=<?php echo $id; ?>"><img src="images/button_add_to_cart.gif" alt="Add to Cart" title=" Add to Cart " width="54"
height="49" /></a></div> </div> </div> </div></div>
<?php
?>
mysql_*() functions return boolean FALSE if they fail, which means your query call did not succeed. Add this code to find out the reason why:
$sql = mysql_query("SELECT * FROM shirts WHERE all='1'");
if ($sql === FALSE) {
die(mysql_error());
}
Here's some sample code:
<?php
$sql = mysql_query("SELECT * FROM shirts WHERE `all`='1'");
if (!$sql ) {
echo "Could not successfully run query from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($sql) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
while ($row = mysql_fetch_assoc($sql))
{
?>
<a href="detail.php?id=<?php echo $row['id']; ?>"><img src="<?php echo $row['picture']; ?>" alt="<?php echo echo $row['title']; ?>" title=" <?php echo $row['title']; ?> " width="190" height="160" />
<?php
}
?>
And here is more info. Notice the exiting php mode inside the while.
I've got a site where someone searches for x product in their location and the site spits back a list of results.
if(isset($_POST['zip'])){
$qry="SELECT business_id FROM ".TBL_BUSINESS." WHERE zip LIKE '%".$_POST['zip']."%'";
$rs = mysql_query($qry);
$rec = array();
while(($row = mysql_fetch_array($rs)) !== FALSE ){
$rec[] = $row[0];
}
if(!empty($rec[0])){
echo "Products for this location<br/>";
foreach ($rec as $result)
{
$bid = $result;
$qry2 = "SELECT * FROM products WHERE business_id = '".$bid."'";
$rs2 = mysql_query($qry2);
$rec2 = mysql_fetch_array($rs2);
?>
<div class="norm">
<img src="admin/product/img/<?php echo $rec2['image']; ?>" height="40" width="40" />
<h3><?echo $rec2['name'];?> <?php echo $rec2['prodvalue']?></h3>
<div class="prodlistMeta">
<a class='view' href="product.php?id=<?php echo $rec2['id']; ?>">View Product</a>
<a class="print" href="#">Print</a>
</div>
</div>
<?php
}
}
else
{
echo "No Product is added for this location";
}
}
?>
What would be the best way to alternate <div class="norm"> with <div class="alt">?
keep a counter and use it's value modulo 2 to determine whether the class should be "norm" or "alt".
$rec2 = mysql_fetch_array($rs2);
$count++;
?>
<div class="<?php echo($count%2?"norm":"alt"); ?>">
I tend to use something like this:
$row = 0;
foreach ($rec as $result) {
$class = $row++ & 1 == 1 ? 'alt' : 'norm';
...
echo <<<END
<div class="$class">
...
END;
}
You can use curly braces to do the expression within the string but I generally prefer not to embed that kind of logic. It's (imho) a little harder to read. Plus the above gives you the opportunity to output it for debugging purposes more easily, etc.
if(some expression)
{
$class="norm";
}
else
{
$class="alt";
}
?>
<div class="<?php echo $class;?>">
set a counter on your output loop. Whenever the counter is even, set the class to normal, else set it to alternate.
Why not use modulus and a row id? Much simpler, no need for unnecessary variables
foreach ($rec as $rid => $result)
{
$bid = $result;
$qry2 = "SELECT * FROM products WHERE business_id = '".$bid."'";
$rs2 = mysql_query($qry2);
$rec2 = mysql_fetch_array($rs2);
?>
<div class="<?=($rid % 2 == 0) ? 'norm' : 'alt' ?>">
<img src="admin/product/img/<?php echo $rec2['image']; ?>" height="40" width="40" />
<h3><?echo $rec2['name'];?> <?php echo $rec2['prodvalue']?></h3>
<div class="prodlistMeta">
<a class='view' href="product.php?id=<?php echo $rec2['id']; ?>">View Product</a>
<a class="print" href="#">Print</a>
</div>
</div>
<?php
}
Widespread adoption of CSS 3 can't come soon enough: http://www.w3.org/TR/2001/CR-css3-selectors-20011113/#structural-pseudos