Hello the valuies are not echoing when I am out side the loop

<?php 
            if(isset($_GET["catid"])) {
                $catid = $_GET["catid"];
                $query_cat = mysqli_query($connection, "SELECT * FROM category WHERE cat_name='$catid'");
                while($row = mysqli_fetch_assoc($query_cat)) {
                    $h1       = $row["h1"];
                    $h2       = $row["h2"];
                    $span     = $row["span"];
                    $catslide = $row["cat_slider"];
                    $ct_name  = $row["cat_name"];
                }
        ?>
            <div class="col-sm-6">
                <h1><?php echo $h1; ?><span class="banner-head"><br><?php echo $h2; ?></span></h1>
                <p><?php echo $span; ?></p>
                <!--<button class="btn btn-default purple-btn" type="submit">downloads</button>-->
            </div>
            <div class="col-sm-6 banner-img">
                <img src="admin/img/postslider/<?php echo $catslide; ?>" alt="">
            </div>
        <?php } else { ?>
            <div class="col-sm-6">
                <h1>Download<span class="banner-head"><br><FREE IPHONE AND <br />ANDROID RINGTONES</span></h1>
                <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry</p>
                <!--<button class="btn btn-default purple-btn" type="submit">downloads</button>-->
            </div>
            <div class="col-sm-6 banner-img">
                <img src="images/mario-game.png" alt="">
            </div>
        <?php } ?>

Recommended Answers

All 6 Replies

you close ur while loop too early, try this

<?php 

                while($row = mysqli_fetch_assoc($query_cat)) {
                    $h1       = $row["h1"];
                    $h2       = $row["h2"];
                    $span     = $row["span"];
                    $catslide = $row["cat_slider"];
                    $ct_name  = $row["cat_name"];
             //}   dont close here
        ?>
        ...
        <?php }} else { ?>//close here

when I am doing this the divs are looping thought which I dotn want it to be done

First of all, had you check your database if last data that matching the condition is having values?
As from what I can see from your code, for every rows of data the mysqli_fetch_assoc fetch, you will replace your $h1,$h2 etc with the newly fetched data. So, if the last row of data is having empty data, then all the variable will be set to empty.

got it the condition was not correct catchable

put ur div outside the loop

Member Avatar for diafol

This makes v. little sense to me.

How many rows are you retrieving? If only one, then you don't need a loop - and I'd add 'LIMIT 1' to the SQL.
If more, then you're overwriting your vars on every iteration of the loop.

Either way, it looks wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.