Page 1 of 1

[SOLVED] empty variables

Posted: Mon Jul 12, 2004 6:53 am
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

Posted: Mon Jul 12, 2004 7:38 am
by Chonk

Code: Select all

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

}

Posted: Mon Jul 12, 2004 9:01 am
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();
}
}
?>

Posted: Mon Jul 12, 2004 11:00 am
by MWG_T_H_O_M_A_S
thanx helps a lot