Page 1 of 1
loop and array trouble
Posted: Sun Dec 01, 2002 7:07 am
by CrazyJimmy
I have the following code which is giving me a parse error on line 8, i have arrays like disc1[], disc2[] etc and am trying to echo the data to the screen. Im sure its my syntax thats the problem.
Code: Select all
<?
//snip
for ($disc_num=1; $disc_num <= $_POSTї'discs'];++$disc_num) {
for ($index=1; $index <= sizeof($disc($disc_num));++$index) {
echo 'Disc '.$disc_num.' Track '.$disc($disc_num)ї$index]; //line 8
}
}
?>
Posted: Sun Dec 01, 2002 7:12 am
by volka
$disc($disc_num)[$index]
do you want to access a two-dimensional array?
Then try
Code: Select all
$discї$disc_num]ї$index]
same with
sizeof($disc($disc_num))
Posted: Sun Dec 01, 2002 7:23 am
by CrazyJimmy
Its not a 2 dimensional array. my forms page creates arrays depending on how many discs there are, disc1 tracks are in the array disc1[], disc2 tracks disc2[] etc
so when im looping i want to like show disc1 track 1, so that would be
$disk1[1] the disc number is not actually in an array.
Sorry if its not clear, im not very good at explaining things.
Posted: Sun Dec 01, 2002 7:42 am
by volka
Posted: Sun Dec 01, 2002 8:38 am
by CrazyJimmy
Thanks, I'm now thinking a 2 dimensional array may actually be better to use.
Posted: Sun Dec 01, 2002 12:08 pm
by CrazyJimmy
this does'nt work. $disc is just a string, not an array or anything
Code: Select all
<?php
sizeof( ${$disc . $disc_num} )
?>
Is there a way to join a string and value of a variable?
My arrays are called $disc1 $disc2 etc depending on how many discs the album has. I want to be able to loop through these by using a loop control variable to change the number on the end only. Also to echo the tracks I want to be able to like $disc.disc_num[track_no]
Hope this is clearer
Posted: Sun Dec 01, 2002 11:33 pm
by volka
ah, I didn't realize that there is no $disc holding the basename of the variable but
disc should be.
But this one is tested

Code: Select all
<?php
$disc1 = array(
'1.About A Girl', '2.Come As You Are', '3.Jesus Doesn''t Want Me For A Sunbeam',
'4.The Man Who Sold The World', '5.Pennyroyal Tea', '6.Dumb', '7.Polly',
'8.On A Plain', '9.Something In The Way', '10.Plateau', '11.Oh Me',
'12.Lake Of Fire', '13.All Apologies', '14.Where Did You Sleep Last Night'
);
$disc2 = array(
'1.Smells Like Teen Spirit', '2.In Bloom', '3.Come As You Are',
'4.Breed', '5.Lithium', '6.Polly', '7.Territorial <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span>',
'8.Drain You', '9.Lounge Act', '10.Stay Away','11.On A Plain',
'12.Something In The Way'
);
for ($disc_num=1; $disc_num <= 2; $disc_num++)
{
for ($index=0; $index < sizeof(${'disc' . $disc_num});$index++)
echo 'Disc ', $disc_num, ' Track ', ${'disc' . $disc_num}ї$index], '<br/>';
}
?>
Posted: Mon Dec 02, 2002 2:03 pm
by CrazyJimmy
Thanks, working fine now, now for the the next problem, im trying to insert into a db, but the variable variable thing is getting me stuck. I thought it would just be:-
Code: Select all
<?php
//db connection stuff done
$sql = "INSERT INTO track (track_pos,name) VALUES('$track_num','${'disc' . $disc_num}[$track_num-1]'";
mysql_query($sql) or die(mysql_error());
?>
im getting error in mysql query near "
Any Ideas?
Thanks
J
Posted: Mon Dec 02, 2002 2:40 pm
by CrazyJimmy
Actually, I have fixed it by doing this :-
Code: Select all
<?php
$track = ${'disc' . $disc_num}[$track_num-1];
$sql = "INSERT INTO track (track_pos,name) VALUES('$track_num','$track')";
mysql_query($sql) or die(mysql_error())
?>
is their anyway lose the extra line by putting the value in the query string?
Code: Select all
<?php
$track = ${'disc' . $disc_num}[$track_num-1];
?>
Posted: Mon Dec 02, 2002 2:59 pm
by horgh
perhaps
Code: Select all
$sql = "INSERT INTO track (track_pos,name) VALUES('$track_num','${{'disc' . $disc_num}[$track_num-1]}'";
?
i think MySQL doesn't like this [$track_num-1] thingie...
Posted: Mon Dec 02, 2002 11:15 pm
by volka
try
Code: Select all
$sql = "INSERT INTO track (track_pos,name) VALUES('$track_num','{${'disc' . $disc_num}[$track_num-1]}')";
see also:
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex
i think MySQL doesn't like this [$track_num-1] thingie...
mysql won't see it
