dbillings Posted October 4, 2007 Share Posted October 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-361310 Share on other sites More sharing options...
teng84 Posted October 4, 2007 Share Posted October 4, 2007 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. ??????????????????????????????????????? Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-361313 Share on other sites More sharing options...
MasterACE14 Posted October 4, 2007 Author Share Posted October 4, 2007 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." ), ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-361367 Share on other sites More sharing options...
teng84 Posted October 4, 2007 Share Posted October 4, 2007 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 />'; } Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-361376 Share on other sites More sharing options...
MasterACE14 Posted October 4, 2007 Author Share Posted October 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-361468 Share on other sites More sharing options...
MasterACE14 Posted October 4, 2007 Author Share Posted October 4, 2007 is it possible to use a switch statement to use with $_POST["type"] and the 3 array variables, $weapons, $armors and $vehicles ? and if it is possible, how would I code it? Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-361603 Share on other sites More sharing options...
MasterACE14 Posted October 5, 2007 Author Share Posted October 5, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-362109 Share on other sites More sharing options...
teng84 Posted October 5, 2007 Share Posted October 5, 2007 switch is use to condition one variable that is posible to have diff value switch($_POST["type"]){ case 'weapon': //statement here break; case 'armors': //statement here break; } Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-362116 Share on other sites More sharing options...
MasterACE14 Posted October 6, 2007 Author Share Posted October 6, 2007 ok, I guess I gotta remove my drop down box feature then :-\ Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363094 Share on other sites More sharing options...
MasterACE14 Posted October 6, 2007 Author Share Posted October 6, 2007 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 ?? Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363114 Share on other sites More sharing options...
teng84 Posted October 6, 2007 Share Posted October 6, 2007 foreach ($weapons as $key => $val) or foreach ($armors as $key => $val) or foreach ($vehicles as $key => $val) that is wrong what do you want to obtain using that code? Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363120 Share on other sites More sharing options...
MasterACE14 Posted October 6, 2007 Author Share Posted October 6, 2007 the 2 dimensional array. Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363196 Share on other sites More sharing options...
MadTechie Posted October 6, 2007 Share Posted October 6, 2007 what are you trying to do ? Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363304 Share on other sites More sharing options...
MasterACE14 Posted October 6, 2007 Author Share Posted October 6, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363379 Share on other sites More sharing options...
MasterACE14 Posted October 6, 2007 Author Share Posted October 6, 2007 ^>bump<^ Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363411 Share on other sites More sharing options...
BlueSkyIS Posted October 6, 2007 Share Posted October 6, 2007 if $_POST["itemtype"] will ALWAYS be a valid array name (or if you validate first), you could use something like: // Define arrays up here. then... $array = ${$_POST['itemtype]}; not tested... Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363429 Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Author Share Posted October 7, 2007 yeah I've tried that with no success Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363641 Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Author Share Posted October 7, 2007 @# Bump #@ Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363793 Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Author Share Posted October 7, 2007 any ideas anyone? Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363917 Share on other sites More sharing options...
yzerman Posted October 7, 2007 Share Posted October 7, 2007 any ideas anyone? K since this is the 3rd page - lets start over: What is the code you are working on? What do you expect it to do? (if you can give me an example of the output you want to see) Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363918 Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Author Share Posted October 7, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363923 Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Author Share Posted October 7, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363925 Share on other sites More sharing options...
yzerman Posted October 7, 2007 Share Posted October 7, 2007 I need some code to work with. I cant tell what you are trying to do with that foreach statement, and going back only confuses me. Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363940 Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Author Share Posted October 7, 2007 <?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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-363943 Share on other sites More sharing options...
MasterACE14 Posted October 8, 2007 Author Share Posted October 8, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/71610-unexpected-t_if/page/2/#findComment-364538 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.