i01573
i01573

Reputation: 87

Show number of search results before php counts them

I am finishing up a search page which shows the search results it finds in my database. Now, I want to display how many it found.

In order to do that I put a counter that gets bigger every time it displays a search result. The problem is that I don't know how to show how many results it found BEFORE it actually displays my search results, considering that I can't echo the counter only after php counts my search results.

Here is how my code looks like:

if(isset($_POST['caut']) && !empty($_POST['caut']))
        {
        $ok1=0;
        $ok2=0;
        $count1=0;
    $count2=0;
        $text=$_POST['caut'];
        $sql="select * from news where content like '%".$text."%' or title like '%".$text."%' order by date desc";
        $result=mysql_query($sql)or die("Unsuccessful".mysql_error());

        #search results displaying start
        while($getNews = mysql_fetch_object($result)){
                $ok1=1;
                $count1++;
                $title = $getNews->title;
                $authorPost = mysql_fetch_array(mysql_query("SELECT * FROM account WHERE id='$getNews->author'"));
                ?>
                <div class="news">
                        <a href="index.php?s=news_page&post=<?php echo $getNews->id; ?>" target="_blank"><?php echo truncatetext($title, 81); ?></a>
                        <?php
                        if (has_rights($rights['add_news'])){
                                echo '<a href="index.php?s=user_panel&a=news&delete='.$getNews->id.'"><div id="delete-button"></div></a>';
                                echo '<a href="index.php?s=user_panel&a=news&edit='.$getNews->id.'"><div id="edit-button"></div></a>';
                        }
                        ?>
                        <p>
                                <div class="news-author">By <a href="index.php?s=user&id=<?php echo $authorPost['id']; ?>"><?php echo $authorPost['real_name']; ?></a></div>
                                <div class="news-date"><img src="images/calendar-icon.png">On <?php echo $getNews->date; ?></div>
                        </p>
                </div>
                <div style="height: 1px; background-color: #cecece; margin-top: 10px; margin-bottom: 0px;"></div>

        <?php
        }

                $sql1="select * from events where content like '%".$text."%' order by date desc";
                $result1=mysql_query($sql1)or die("Unsuccessful".mysql_error());
                while($getEvents = mysql_fetch_object($result1)){
                        $ok2= 1;
                        $count2++;
                        $title = $getEvents->title;
                        $authorPost = mysql_fetch_array(mysql_query("SELECT * FROM account WHERE id='$getEvents->author'"));
                        ?>
                        <div class="news">
                                <div class="event-date" align="center">
                                        <div class="event-day"><?php echo $getEvents->event_day; ?></div>
                                        <div class="event-month"><?php echo $getEvents->event_month.'.'.$getEvents->event_year; ?></div>
                                </div>
                                        <a href="index.php?s=events_page&post=<?php echo $getEvents->id; ?>"><?php echo truncatetext($title, 36); ?></a>
                                <?php
                                if (has_rights($rights['add_news'])){
                                        echo '<a href="index.php?s=user_panel&a=events&delete='.$getEvents->id.'"><div id="delete-button"></div></a>';
                                        echo '<a href="index.php?s=user_panel&a=events&edit='.$getEvents->id.'"><div id="edit-button"></div></a>';
                                }
                                ?>
                        </div>
                        #search results displaying end


                <?php
                }      

                        #I want the following TWO lines showed BEFORE my search results are being displayed
                        $nr_rez= $count1 + $count2;
                        echo '<br><br><div class="info">Found '.$nr_rez.' results matching "'.$text.'".</div>';


                        if($ok1==0 && $ok2==0)
                        echo '<div class="error">No results found with that keyword.</div>';

}

Is this possible to be done?

Upvotes: 0

Views: 85

Answers (1)

Thomas Landauer
Thomas Landauer

Reputation: 8355

Sure, use mysql_num_rows(): http://php.net/manual/en/function.mysql-num-rows.php

Besides, I would switch to mysqli, since mysql will be dead soon.

And never use any user input directly without sanitizing it (like your $_POST['caut']). Google for 'sql injection'.

Upvotes: 1

Related Questions