Jump to content

easy way to echo part of an array?


TheStalker

Recommended Posts

Hi all,

 

I have two dynamic vars as below

$var1 = "car1";
$var = "car4";

 

I also have any array of cars 1-5

$cars=array("car1","car2","car3","car4","car5"); 

 

What i want to be able to do is echo out in a list the cars that are not in my vars

 

So from the example i gave i would like to echo

 

car2

car3

car5

 

Is there an easy way this can be done ? Ive tried a few dif ways but with no luck so far. Like i said the vars could change so could be any of the cars.

 

any helpwould be graet thanks

 

 

 

Link to comment
Share on other sites

Hi all,

 

I have two dynamic vars as below

$var1 = "car1";
$var = "car4";

 

I also have any array of cars 1-5

$cars=array("car1","car2","car3","car4","car5"); 

 

What i want to be able to do is echo out in a list the cars that are not in my vars

 

So from the example i gave i would like to echo

 

car2

car3

car5

 

Is there an easy way this can be done ? Ive tried a few dif ways but with no luck so far. Like i said the vars could change so could be any of the cars.

 

any helpwould be graet thanks

 

Try:

 

for($i = 0; $i < count($cars); ++$i)
{
   if($cars[$i] != $var || $cars[$i] != $var1) { echo $cars[$i]; }
}

Link to comment
Share on other sites

Hi all,

 

I have two dynamic vars as below

$var1 = "car1";
$var = "car4";

 

I also have any array of cars 1-5

$cars=array("car1","car2","car3","car4","car5"); 

 

What i want to be able to do is echo out in a list the cars that are not in my vars

 

So from the example i gave i would like to echo

 

car2

car3

car5

 

Is there an easy way this can be done ? Ive tried a few dif ways but with no luck so far. Like i said the vars could change so could be any of the cars.

 

any helpwould be graet thanks

 

Try:

 

for($i = 0; $i < count($cars); ++$i)
{
   if($cars[$i] != $var || $cars[$i] != $var1) { echo $cars[$i]; }
}

 

hi this seems to just echo all 5 cars

Link to comment
Share on other sites

The following works:

 

<?php
   $cars = array("car1", "car2", "car3", "car4", "car5");

   $firstTest = "car2";
   $secondTest = "car5";

   for($i = 0; $i < count($cars); ++$i)
   {
      if ($cars[$i] != $firstTest && $cars[$i] != $secondTest) { echo $cars[$i]; }
   }
?>

 

It outputs:

 

car1car3car4
Link to comment
Share on other sites

ok thanks for that,

 

now what im trying do is do the same thing from a query on a database. Im  having a bit of trouble of working out howto get the cars into the vars tho 

 

i know i have to loop round but cant work out how it should be done

 

my db might look somthing like this:

date -car

30/01/2010 - car1

27/03/2010 -car3

27/03/2010 - car4

04/05/2010 - car1

06/06/2010 -car5

06/06/2010 - car4

 

 

 

$sql = mysql_query("SELECT * FROM bookings WHERE datebooking ='2010-03-27' ");


$i=0; 
while($row=mysql_fetch_array($sql))
{
	$i++;
	$car.$i == $row['car']; 
	//echo $row['car'];
}

Link to comment
Share on other sites

Use the vars that hold the car names to improve upon the MYSQL query to filter out the database results you don't want.

$var1 = 'car1';
$var2 = 'car2';
$query = "SELECT * FROM bookings WHERE datebooking ='2010-03-27' AND carName != '".$var1."' AND carName != '".$var2"'";
$sql = mysql_query($query);

For this solution, it would be better to stored the filtered car names in an array so a foreach loop can be used to add the exceptions to the MYSQL query. That way your code will dynamically generate the query string no matter how many or little cars are being filtered.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.