Jump to content

Recommended Posts

Can anyone assist with this?    I need the code to select between 2 arrays and then display the results accordingly but it's not working...

 

	$arr_tower = "";

    if ($FOLDER == "101") $arr_tower = ('$kba');
if ($FOLDER == "201") $arr_tower = ('$kba');
if ($FOLDER == "301") $arr_tower = ('$uni');
if ($FOLDER == "401") $arr_tower = ('$uni');

echo 'FOLDER = '. $FOLDER . ' so Tower = '. $arr_tower;
echo ' --- foreach ('.$arr_tower.' as $key=>$tower)';

$kba = array(0=>"U1/2", 2=>"T2A", 3=>"U3", 4=>"U5/6", 5=>"T7");
$uni = array(0=>"T1", 1=>"T2", 2=>"T3", 3=>"T4", 4=>"T5", 5=>"T6", 6=>"T7", 7=>"T8", 8=>"T9", 9=>"T10"); 

echo "<table>";
foreach ($arr_tower as $key=>$tower)
{
$k = key($arr_tower); 
$val = $key + '15';
echo '<tr><td>'. $key .' => '. $tower. ' = '. $val .' --> '. $pbupdate[$val].'</td></tr>';
};
echo "</table>";

 

Got to be something simple.  If I just use one array and the array name rather than $arr_tower it works fine.  (btw - I'm adding 15 to $key because the db query result for this section starts at $pbupdate[15] and it works.)

 

This bit I added in as a test to see what exactly was being produced-

echo 'FOLDER = '. $FOLDER . ' so Tower = '. $arr_tower;
echo ' --- foreach ('.$arr_tower.' as $key=>$tower)';

- produces the correct string...

 

FOLDER = 401 so Tower = $uni --- foreach ($uni as $key=>$tower)

 

...however the web page shows this error:

 

"Warning: Invalid argument supplied for foreach() in C:\wamp\www\ProdBoard\pb_job_update.php on line 245"

 

...which is the actual foreach() line.    I've tried moving the arrays to the top of the section; to where they are now; all oevr the place...  is there something I'm missing here?

 

 

foreach ($arr_tower as $key=>$tower)

 

gives:

 

foreach ('$uni' as $key => $tower)

 

which gives you:

Warning: Invalid argument supplied for foreach() in C:\wamp\www\ProdBoard\pb_job_update.php on line 245

 

use the next technique instead (http://us3.php.net/manual/en/language.variables.variable.php):

if ($FOLDER == "101") $arr_name = 'kba';
if ($FOLDER == "201") $arr_name = 'kba';
if ($FOLDER == "301") $arr_name = 'uni';
if ($FOLDER == "401") $arr_name = 'uni';

$kba = array(0=>"U1/2", 2=>"T2A", 3=>"U3", 4=>"U5/6", 5=>"T7");
$uni = array(0=>"T1", 1=>"T2", 2=>"T3", 3=>"T4", 4=>"T5", 5=>"T6", 6=>"T7", 7=>"T8", 8=>"T9", 9=>"T10"); 

$arr_tower = ${$arr_name};
foreach ($arr_tower as $key => $tower) {
    ..

try

<?php
$arr_tower = "";
//$FOLDER=401;
    if ($FOLDER == "101") $arr_tower = ('kba');
if ($FOLDER == "201") $arr_tower = ('kba');
if ($FOLDER == "301") $arr_tower = ('uni');
if ($FOLDER == "401") $arr_tower = ('uni');

echo 'FOLDER = '. $FOLDER . ' so Tower = '. $arr_tower;
echo ' --- foreach ('.$arr_tower.' as $key=>$tower)';

$kba = array(0=>"U1/2", 2=>"T2A", 3=>"U3", 4=>"U5/6", 5=>"T7");
$uni = array(0=>"T1", 1=>"T2", 2=>"T3", 3=>"T4", 4=>"T5", 5=>"T6", 6=>"T7", 7=>"T8", 8=>"T9", 9=>"T10"); 

echo "<table>";
foreach ($$arr_tower as $key=>$tower)
{
$k = key($$arr_tower); 
$val = $key + '15';
echo '<tr><td>'. $key .' => '. $tower. ' = '. $val .' --> '. $pbupdate[$val].'</td></tr>';
};
echo "</table>";
?>

Sasa's one worked - in a test output I got

 

FOLDER = 401 so Tower = uni --- foreach (uni as $key=>$tower)0 => T1 = 15 --> 0

1 => T2 = 16 --> 0

2 => T3 = 17 --> 0

3 => T4 = 18 --> 0

4 => T5 = 19 --> 0

5 => T6 = 20 --> 0

6 => T7 = 21 --> 0

7 => T8 = 22 --> 0

8 => T9 = 23 --> 0

9 => T10 = 24 --> 0

 

..and so I can do stuff with that.    Ignace - sorry - your one produced the same error, but they've both helped me a lot.  Never come across variable variables before - but then I'm no web programmer, just a sysadmin, but learning and using php to write tools for the job - and enjoying it (mostly).

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.