June 18, 2013

How to hide Partial Data in PHP

Sameer007860’s Question:

Eperimenting PHP just for fun, But As being newbie, I’m unable to understand curcial parts of PHP….Please help me to sort out this problem which I’m explaining by example :

Suppose

$sql = "SELECT id, text,uid FROM feeds WHERE uid='".$ud."' LIMIT 10";
$items = mysql_query($sql);
echo mysql_error();

if (@mysql_num_rows($items) > 0)
{
    while ($item = mysql_fetch_array($items))
    {
        $feed = $item[1];
        $nick = getnick($item[2]);
    }
}

So I want to display like this :

3 Records with uid details…

jay,vicky, sumair and 17 others like this.

Please help me to get output of something like this !!

Thanks !!

I can’t stretch this enougth,

DO NOT USE MYSQL_* API anymore. [Read this]

It is VULNERABLE, mysqli_* functions are just as similar very little difference.

And You already are doing the things required for that output mysql_num_rows() already gives the number of total result. So:

if (@mysql_num_rows($items) > 0)
{
    $count = mysql_num_rows($items);
    echo $count." Records with uid details..."; //Display the count of records

    $threeNameHolder = array; // Hold the first three names on this

    while ($item = mysql_fetch_array($items))
    {
        $feed = $item[1];
        $nick = getnick($item[2]);
        if(count($threeNameHolder) < 3) {
            $threeNameHolder[] = $nick;
        } else break; // End the loop here
    }

    //Now display the name
    echo implode(",", $threeNameHolder). " and ".($count - 3)." others like this.";
}

Safer and MYSQLi Version

if (@mysqli_num_rows($items) > 0)
{
    $count = mysqli_num_rows($items);
    echo $count." Records with uid details..."; //Display the count of records

    $threeNameHolder = array; // Hold the first three names on this

    while ($item = mysqli_fetch_array($items))
    {
        $feed = $item[1];
        $nick = getnick($item[2]);
        if(count($threeNameHolder) < 3) {
            $threeNameHolder[] = $nick;
        } else break; // End the loop here
    }

    //Now display the name
    echo implode(",", $threeNameHolder). " and ".($count - 3)." others like this.";
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!