Jump to content

Recommended Posts

I only glanced at the code but I think using $array without the [] will only see it as a variable and change the value each time.

You want to use $array[]. This will produce a true array with multiple values.

 

 

 

???????????????????????????????????????

The arrays are in this same structure. Here is the $weapons one.

<?php // Weapons

$weapons = array( array( id => 0,
					 weapon => "L85A2", 
					 price => 250,
					 damage => 15,
					 type => "Rifle",
					 rarity => "Basic",
					 description => "The L85A2 is a bullpup assault rifle, it is very powerful and a 
           				                 iron-sight comes standard.",
					 options => "You can give it a Laser."
					),
		   array( id => 1,
					 weapon => "AK-47", 
					 price => 325,
					 damage => 25,
					 type => "Rifle",
					 rarity => "Good",
					 description => "The AK-47(Kalashnikov) is a Russian made assault rifle, it is one of the most
						                 popular weapons in the world, and is also one of the most reliable.",
					 options => "You can give it a iron-sight."
					),
               array( id => 2,
					 weapon => "L96", 
					 price => 400,
					 damage => 30,
					 type => "Sniper Rifle",
					 rarity => "Good",
					 description => "The L96(super magnum) is a sniper rifle with deadly accuracy.",
					 options => "You can give it a stand."
					),
             );

?>

foreach($array as $key => $val){
if($item_search == $val[$item_cr]){
	$search = 1;
		echo '<table class="fix"><tr>';
		if($item_type == "weapons"){
			$x= 'weapon';
		}
		elseif($item_type == "armors"){
			$x= 'armor';
		}
		elseif($item_type == "vehicles"){
			$x= 'vehicle';
		}
		echo '<td colspan="2">' . $val[$x]. '</td></tr>';
		echo '<tr><td>id</td><td>' . $val['id'] . '</td></tr>';
		if($item_type == "weapons"){
			$y= 'damage';
		}
		elseif($item_type == "armors"){
			$y= 'defence';
		}
		elseif($item_type == "vehicles"){
			$y= 'power';
		}
		echo '<tr><td>' . $x . '</td><td>' . $val[$x] . '</td></tr>';
		echo '<tr><td>price</td><td>' . $val['price'] . '</td></tr>';
		echo '<tr><td>' . $y . '</td><td>' . $val[$y] . '</td></tr>';
		echo '<tr><td>type</td><td>' . $val['type'] . '</td></tr>';
		echo '<tr><td>rarity</td><td>' . $val['rarity'] . '</td></tr>';
		echo '<tr><td>description</td><td>' . $val['description'] . '</td></tr>';
		echo '<tr><td>options</td><td>' . $$val['options'] . '</td></tr>';
		echo '</table><br />';
}

 

The part you just gave me already works fine. Its the problem with the 3 if statements before that. The IF statements try to set the array variable, either $weapons, $armors or $vehicles. And put those array variables into a

normal variable called $array. But the foreach function isnt recognising $array as one of the array variables.

on second thoughts, can I do something like this?

<?php
foreach ($weapons as $key => $val) or
foreach ($armors as $key => $val) or
foreach ($vehicles as $key => $val)

 

or this?

<?php
foreach ($weapons as $key => $val) and
foreach ($armors as $key => $val) and
foreach ($vehicles as $key => $val)

 

or do I have to use the other operators?

<?php
|| // or
&& // and

 

??

I have a search form, which searchs a 2 dimensional array for items that are in the array. I have 3 separate 2d arrays. Which are called $weapons, $armors and $vehicles, now the script I have for displaying the found result from the array was working fine, when I had just the 1, 2d array for it to search. But where I have the foreach statement:

<?php
foreach ($weapons as $key => $val)

 

I need the $weapons to automatically change to what array is being searched, depending on what has been selected in the dropdown box on the previous page.

 

so I have tried if statements, like so:

<?php
if($_POST["itemtype"] == "weapons") {
$array = $weapons;
} // but with 1 if statement for each array of course
foreach ($array as $key => $val)

 

and I have thought of maybe a switch statement, and my latest one, using "or" and "and".

 

But I have had no luck so far.  :-[

Basically I have a cronjob, set to run every 60 minutes(hourly) and this is the file that is run by the cronjob:

<?php // cron job

include_once '/home/ace/public_html/conflictingforces/functions.php';

lastturn(time());
nextturn();

?>

 

Now the 2 functions I run in the cronjob, are for telling users on my website how long it is until the next one is going to run. Here is the 2 functions I have for that:

<?php
// Workout the time since the last turn change
function lastturn($time){

$rs = mysql_connect( "localhost", "ace_ACE", "shadow69" );
$rs = mysql_select_db( "ace_cf" );

// SQL query for all entries in descending order
$sql = "UPDATE `cf_info` SET lastturn = $time";
echo $sql;
$rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());
}


// Workout the time until the next turn change
function nextturn(){

$rs = mysql_connect( "localhost", "ace_ACE", "shadow69" );
$rs = mysql_select_db( "ace_cf" );

// SQL query for all entries in descending order
$sql = "SELECT `lastturn` FROM `cf_info`";
$SQLq = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());
$rss = mysql_fetch_assoc( $SQLq );
$lastturntime = $rss['lastturn'];

// grab time since last turn change
$lastturnchange = $lastturntime[0];
// get the current time
$currenttime = time();
// get the difference between last turn change and the current time
$dif = $currenttime - $lastturnchange;
// workout how many minutes until the next turn : part 1
$nextturnmin = ceil($dif/60); //round up (remove the seconds)
// workout how many minutes until the next turn : part 2
$nextturn = 60 - $nextturnmin;
// if the time until the next turn is less then zero, set the time till next turn to 0
if ($nextturnmin < 0) { $nextturnmin = 0; }
// return the time until next turn change
return $nextturnmin;
}
?>

 

Basically I want it to display how long it is until the next cronjob in minutes. It is currently displaying this:

Next Turn in 19862284 min

 

LOL! wrong post altogether sorry. that post is here http://www.phpfreaks.com/forums/index.php/topic,162011.15.html

 

let me start again

what I have is a 2dimensional array, and I have a foreach statement for each key and value in the array ready to be echo'd into a table. Basically I have a form on the previous page, which has a drop down box, with 3 options, they are "weapon", "armor" and "vehicle". These are the 3, 2d arrays I have setup "$weapons", "$armors" and "$vehicles". And with the foreach statement, it is setup like this:

foreach($array as $key => $val)

 

and $array is whatever array was selected with the drop down box. But it doesn't work, I need to somehow work out a way to get whatever option was chosen, and put it into a variable called $array so it can be used in the foreach statement.

 

Regards ACE

<?php
$item_cr = $_POST["cat"]; // this gets the checkbox value
$item_search = $_POST["item"]; // this gets the search box value
$item_type = $_POST["type"]; // this gets the drop down box value

if($item_type == "weapons") {
$array = $weapons;
}
elseif($item_type == "armors") {
$array = $armors;
}
elseif($item_type == "vehicles") {
$array = $vehicles;
}

foreach($array as $key => $val){
if($item_search == $val[$item_cr]){
	$search = 1;
		echo '<table class="fix"><tr>';
		if($item_type == "weapons"){
			$x= 'weapon';
		}
		elseif($item_type == "armors"){
			$x= 'armor';
		}
		elseif($item_type == "vehicles"){
			$x= 'vehicle';
		}
		echo '<td colspan="2">' . $item_type[$key][$x]. '</td></tr>';
		echo '<tr><td>id</td><td>' . $item_type[$key]['id'] . '</td></tr>';
		if($item_type == "weapons"){
			$y= 'damage';
		}
		elseif($item_type == "armors"){
			$y= 'defence';
		}
		elseif($item_type == "vehicles"){
			$y= 'power';
		}
		echo '<tr><td>' . $x . '</td><td>' . $item_type[$key][$x] . '</td></tr>';
		echo '<tr><td>price</td><td>' . $item_type[$key]['price'] . '</td></tr>';
		echo '<tr><td>' . $y . '</td><td>' . $item_type[$key][$y] . '</td></tr>';
		echo '<tr><td>type</td><td>' . $item_type[$key]['type'] . '</td></tr>';
		echo '<tr><td>rarity</td><td>' . $item_type[$key]['rarity'] . '</td></tr>';
		echo '<tr><td>description</td><td>' . $item_type[$key]['description'] . '</td></tr>';
		echo '<tr><td>options</td><td>' . $item_type[$key]['options'] . '</td></tr>';
		echo '</table><br />';
}

}
if(!isset($search) || $search != 1){
echo "not found<br><br>";
}

?>

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.