-1

Hi I have the following code. When the data is displayed on the screen blank areas are also printed. How can I adjust/change my code to eliminate blank spaces when records are displayed. Thanks you.

        <?php

$con=mysql_connect('localhost','root') or die ("Server connection failure!");
$db=mysql_select_db('regional_data',$con) or die ("Couldn't connect the database");
$SQLa="SELECT * FROM newchk WHERE dist_chk='$distUsr'";
$runa=mysql_query($SQLa,$con) or die ("SQL Error");
$nora=mysql_num_rows($runa);

echo "<table cellpadding='2' class='tablet' cellspacing='0'>";
echo 
"<tr>"
."<th>"."Starting Cheque No"."</th>"
."<th>"."Ending Cheque No"."</th>"
."<th>"."Total No of Cheques"."</th>"
."</tr>";

    while ($reca = mysql_fetch_array($runa))
    {

        echo "<tr>";
        echo "<td>".$reca["sbstart"]."</td>";
        echo "<td>".$reca["sbend"]."</td>";
        echo "<td>".$reca["totsb"]."</td>";
        echo "</tr>";
    }
echo "</table>";
?>
Stack One
  • 25
  • 2
  • 10
  • 2
    You are using [an obsolete database API](http://stackoverflow.com/q/12859942/19068) – Quentin Oct 19 '12 at 07:03
  • Well, you could _check_ if the fields are blank and don't display them in that case, or simply filter them out in your query. Are you really asking for a tutorial in basic conditional statements and `WHERE` conditions? – lanzz Oct 19 '12 at 07:03
  • Is the PHP generating the HTML you expect, but the HTML is giving you the blank areas? If so, show us the HTML, not the PHP. – Quentin Oct 19 '12 at 07:04
  • Is the PHP generating HTML that isn't the way you want it, and the differences between how you want the HTML and how the HTML is are causing the blank areas? If so, show us the HTML and the PHP and tell us what is wrong with the source, not the rendering. – Quentin Oct 19 '12 at 07:05
  • @Quentin What I meant by 'Blank areas' are the black spaces in the SQL database. So If one row has no data,once the data is displayed space is also printed and then the next row's data. So in a sequence I get data and blank spaces displayed. Hope this clarifies. Thanks – Stack One Oct 19 '12 at 07:19

3 Answers3

1
 while ($reca = mysql_fetch_array($runa))
    {

        echo "<tr>";
        echo "<td>".trim($reca["sbstart"])."</td>";
        echo "<td>".trim($reca["sbend"])."</td>";
        echo "<td>".trim($reca["totsb"])."</td>";
        echo "</tr>";
    }
Dilip Godhani
  • 2,065
  • 3
  • 18
  • 33
1

I didn't properly understand what did you want to say but if you didn't want to print blank records then use this Use one of condition Either first or Second


     while ($reca = mysql_fetch_array($runa))
        { 
            if($reca["sbstart"] || $reca["sbstart"] || $reca["sbstart"]){  //If any of record is empty
            echo "";
            echo "".$reca["sbstart"]."";
            echo "".$reca["sbend"]."";
            echo "".$reca["totsb"]."";
            echo "";
            }
           if($reca["sbstart"] && $reca["sbstart"] && $reca["sbstart"]){  //If all of row in record is empty
            echo "";
            echo "".$reca["sbstart"]."";
            echo "".$reca["sbend"]."";
            echo "".$reca["totsb"]."";
            echo "";
            }
        }

Or if you want to trim spaces then use this


     while ($reca = mysql_fetch_array($runa))
        {

            echo "";
            echo "".trim($reca["sbstart"])."";
            echo "".trim($reca["sbend"])."";
            echo "".trim($reca["totsb"])."";
            echo "";
        }

I advice you to use mysqli_* or pdo instead of mysql_* as it is deprecated

Umair Aziz
  • 1,518
  • 1
  • 19
  • 29
0

Simply use the function below,

function fetch_value($string) 
{    
        $string = trim($string);
        $string = mysql_real_escape_string($string);
        $string = stripslashes($string);
        return $string;
}

this will help us to remove white spaces as well as help u to stop sql injection as well.

Vee Mandke
  • 578
  • 1
  • 11
  • 28