[SOLVED] empty variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MWG_T_H_O_M_A_S
Forum Newbie
Posts: 7
Joined: Mon Jun 28, 2004 10:33 am

[SOLVED] empty variables

Post by MWG_T_H_O_M_A_S »

ok ive ran into a slight problem, my table writes variables to a txt file, which are then read from in another page, the code for reading te variables is as follows.

$car is the variable passed to the page, from another.

Code: Select all

<?php
$filename = $car; 
$fd = fopen ($filename, "r"); 
$contents = fread ($fd, filesize ($filename)); 
$variable = explode(',',$contents);

	$name =  $variable[0];
	$country = $variable[1];	
	$email = $variable[2];
	$manufacturer = $variable[3];
	$model = $variable[4];
	$powertrain = $variable[5];
	$baseprice = $variable[6];
	$engine = $variable[7];
	$transmission = $variable[8];
	$suspension = $variable[9];
	$brakes = $variable[10];
	$wheels = $variable[11];
	$tyres = $variable[12];
	$exterior = $variable[13];
	$interior = $variable[14];
	$security = $variable[15];
	$figures = $variable[16];
	$otherinfo = $variable[17];
	$image1 = $variable[18];
	$image2 = $variable[19];
	$image3 = $variable[20];
	$image4 = $variable[21];

fclose ($fd); 
?>
I am now at the stage where if the variable ( all txt strings) are empty ( the field on the form was not filled in) then it will replace the txt with something else eg "not available", so i added this code, for instance with $country.

Code: Select all

<?php

if ($country == "")
{
		$country = "<font face="Verdana, Arial, Helvetica, sans-serif" color="#cccccc" size="2">Not Available</font>";
}

?>
The trouble is, to do this for every variable will make a huge messy script, is there any much shorter way i can do this, maybe using a while loop? or something like that?.

Thanx

Tom
Chonk
Forum Newbie
Posts: 24
Joined: Fri May 28, 2004 3:58 am

Post by Chonk »

Code: Select all

foreach ($variable as $v)
{
  if(empty($v))
 {
   $v = "not available";
 }

}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?
if (!empty($_POST["submit"]
{

foreach ($variable as $v) 
{ 
  if(empty($v)) 
{ 
   $v .= "not available <br>"; 
   $error += 1;
} 

} 

if (!empty($error))
{
   echo $v;
}
else
{
echo form();
}
}
?>
MWG_T_H_O_M_A_S
Forum Newbie
Posts: 7
Joined: Mon Jun 28, 2004 10:33 am

Post by MWG_T_H_O_M_A_S »

thanx helps a lot
Post Reply