how to eliminate empty spaces

Discussion in 'PHP' started by zed420, Dec 26, 2008.

  1. #1
    Hi All
    I wonder if someone can help me, I have a form where a customer has an option to book 1 or 2 vehicles by dropdown box so I’m using show/hide div for this which is working fine inserting the data all ok but when it comes to echo data it isn’t a pretty picture specially when I’m showing for two vehicles. Because there are empty spaces in database the table is not lining up properly. Is there any way I can get rid of these empty spaces when it comes to echo?
    This is how I’m inserting the data;
     	  if ($_POST['noOfVehicle'] == "1" ) {		
    $query = "INSERT INTO restHotel  VALUES
    ('$status',NULL,'$dateTime','$passType','$noOfVehicle','$custname1','$des1','$roomNo1','$tableNo1','$vehicleType1','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','')";
       		if (@mysql_query($query)) {
    			echo '<p>Your vehicle request has been sent Successfully </p>';
    			} else {
    			echo '<p>Error adding submitted Information: ' .
    			mysql_error() . '</p>';
    		}   
    }
    /************************************** Vehicle 2 ************************************/
    	  if ($_POST['noOfVehicle'] == "2" ) {		
    $query = "INSERT INTO restHotel  VALUES
    ('$status',NULL,'$dateTime','$passType','$noOfVehicle','','','','','','$custname2','$des2','$roomNo2','$tableNo2','$vehicleType2','$custname3','$des3','$roomNo3','$tableNo3','$vehicleType3','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','')";
       		if (@mysql_query($query)) {
    			echo '<p>Your vehicle request has been sent Successfully </p>';
    			} else {
    			echo '<p>Error adding submitted Information: ' .
    			mysql_error() . '</p>';
    		}   
    } 
    Code (markup):
    This is how I'm out putting it.
    $query = "SELECT * FROM restHotel ORDER BY id DESC LIMIT 50";
       $result = mysql_query($query)or die(mysql_error());
    
     	while ($row = mysql_fetch_array($result))  {
        extract($row);
    		echo  "<tr >
    		<td align=center>" . $row['id'] . "</td>
    		<td align=center>" . $row['dateTime'] . "</td>
    		<td align=center>" . $row['passType'] . "</td>
    		<td align=center>" . $row['noOfVehicle'] . "</td>
                             <td align=center>" . $row['custname1'] . "</td>
    		<td align=center>" . $row['des1'] . "</td>
    		<td align=center>" . $row['roomNo1'] . "</td>
    		<td align=center>" . $row['tableNo1'] . "</td>
    		<td align=center>" . $row['vehicleType1'] . "</td></tr>";
    echo "<tr><td></td><td></td><td></td><td></td>	
    		<td align=center>" . $row['custname2'] . "</td>
    		<td align=center>" . $row['des2'] . "</td>
    		<td align=center>" . $row['roomNo2'] . "</td>
    		<td align=center>" . $row['tableNo2'] . "</td>
    		<td align=center>" . $row['vehicleType2'] . "</td></tr>";
    echo "<tr>	<td></td><td></td><td></td><td></td>	
    		<td align=center>" . $row['custname3'] . "</td>
    		<td align=center>" . $row['des3'] . "</td>
    		<td align=center>" . $row['roomNo3'] . "</td>
    		<td align=center>" . $row['tableNo3'] . "</td>
    		<td align=center>" . $row['vehicleType3'] . "</td></tr>";		
    	}
    	?></table></div>
    Code (markup):
    Thanks
    Zed
     
    zed420, Dec 26, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Bit vague, but I'm sure I'll list what you need:

    Way 1: Remove spaces from either end.
    trim($string);
    PHP:
    Way 2: Remove ALL spaces.
    strtr($string, array(' ' => ''));
    PHP:
    Way 3: Remove all spaces of 2 or more.
    preg_replace('/\s{2,}/', ' ', $string)
    PHP:
    Also...
        extract($row);
    
    PHP:
    Why are you bothering to do this if you're going to use array indexes afterwards anyway?
     
    Danltn, Dec 26, 2008 IP
  3. zed420

    zed420 Member

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    This what it looks like[​IMG].
     

    Attached Files:

    • doc.jpg
      doc.jpg
      File size:
      27.6 KB
      Views:
      40
    zed420, Dec 26, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Add this before extract($row);

    array_walk($row, create_function('&$value, $key', 'if(!$value) $value = \'&nbsp;\';'));
    PHP:
     
    Danltn, Dec 26, 2008 IP
  5. zed420

    zed420 Member

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Thanks for your reply, It doesn't quite work it even shows the empty celles

    thanks
    Zed
     
    zed420, Dec 26, 2008 IP