Omar
Omar

Reputation: 13

Some HTML tags disappear when i use PHP

I'm trying to generate dynamic contents fill from mysql db here is my php code:

<?php 
    include 'header.php'; 
    error_reporting(1);
    $user = "root";
    $pass = "";
    $dbName = "haimi";
    mysql_connect('localhost', $user, $pass) or
        die("Could not connect: " . mysql_error());
    mysql_select_db($dbName);
    $sql = "SELECT * FROM Projects ";
    $result = mysql_query($sql);
?>

<?php
    while ($line = mysql_fetch_array($result)) {
?>
        <li class="filter" data-filter=".cat<?php echo $line['Category'];?>"><?php echo $line['Category'];?></li>
<?php 
    } 
?>

The li displays correctly, but the following does not:

<div class="row projects m0">
<?php
    while ($line = mysql_fetch_array($result)) { ?>
        <div class="project mix catHouses">
            <div class="tint"></div>
            <a href="images/projects/".<?php echo $line['ProjectImage1']; ?> data-    
lightbox="project" data-title="Central Hospital (building)">
            <img src="images/projects/".<?php echo 
$line['ProjectImage1']; ?> alt="<?php echo $line['ProjectTitle'];?>"   
class="projectImg"> </a>
            <div class="projectDetails row m0">
                <div class="fleft nameType">
                    <div class="row m0 projectName"><?php echo $line['ProjectTitle'];?></div>
                    <div class="row m0 projectType"><?php echo $line['ProjectType'];?></div>
                </div>
                <div class="fright projectIcons btn-group" role="group">
                    <a href="images/projects/<?php echo $line['ProjectImage1']; ?>" data-lightbox="project" data-title="Central Hospital   (building)" class="btn btn-default">
                    <i class="fa fa-link"></i></a>
                    <a href="#" class="btn btn-default"><i class="fa fa-    search"></i></a>
                </div>
            </div>
        </div>
<?php
    }
?>
</div>  

It data in the divs doesn't appear.

Upvotes: 0

Views: 569

Answers (4)

Omar
Omar

Reputation: 13

Like what Mr @matthew said

I was making a single call, and I was trying to loop through it twice.

The problem solved with this code before the while loop:

$result = mysql_query($sql);

Upvotes: 0

dashtinejad
dashtinejad

Reputation: 6263

You have some HTML mistakes here:

<a href="images/projects/".<?php echo $line['ProjectImage1']; ?>

First, there is no need to use . operator (as you are in HTML, not PHP), Also you shoud put your <?php ?> tag inside the href quotations, here is the correct code:

<a
    href="images/projects/<?php echo $line['ProjectImage1']; ?>"
    data-lightbox="project"
    data-title="Central Hospital (building)"
>
    <img
        src="images/projects/<?php echo $line['ProjectImage1']; ?>"
        alt="<?php echo $line['ProjectTitle']; ?>"   
        class="projectImg"
    >
</a>

Upvotes: 1

Matthew Johnson
Matthew Johnson

Reputation: 5165

You're making a single call, but trying to loop through it twice. To do so, you need to reset the pointer back to the beginning:

//Add this after the first loop, but before the second
mysql_data_seek( $result, 0 );

The way you have it now, it's while($line = mysql_fetch_array($result)), but the second loop is never entered since it has already reached the end. Since the loop is ended, it never displays the contents.

Important Note The mysql_* functions are deprecated, and is removed in PHP 5.5. You should use Mysqli or PDO. They have better protections against mysql injections (see Bobby Tables).

Upvotes: 1

Pedro Lobito
Pedro Lobito

Reputation: 99041

You will get older fast writing code like that ;) How about this:

while ($line = mysql_fetch_array($result)) {
    $category = $line['Category'];
    echo <<< LOB
<li class="filter" data-filter="$category">$category</li>
LOB;
}

Upvotes: 0

Related Questions