loop and array trouble

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CrazyJimmy
Forum Commoner
Posts: 34
Joined: Tue Nov 19, 2002 1:40 pm

loop and array trouble

Post 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
}
}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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)) :arrow:

Code: Select all

sizeof($discї$disc_num])
CrazyJimmy
Forum Commoner
Posts: 34
Joined: Tue Nov 19, 2002 1:40 pm

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ah...then you are interested in http://www.php.net/manual/en/language.v ... riable.php

Code: Select all

sizeof( ${$disc . $disc_num} )
CrazyJimmy
Forum Commoner
Posts: 34
Joined: Tue Nov 19, 2002 1:40 pm

Post by CrazyJimmy »

Thanks, I'm now thinking a 2 dimensional array may actually be better to use.
CrazyJimmy
Forum Commoner
Posts: 34
Joined: Tue Nov 19, 2002 1:40 pm

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 &lt;= 2; $disc_num++)
{
	for ($index=0; $index &lt; sizeof(${'disc' . $disc_num});$index++)
		echo 'Disc ', $disc_num, ' Track ', ${'disc' . $disc_num}&#1111;$index], '&lt;br/&gt;';
} 
?&gt;
CrazyJimmy
Forum Commoner
Posts: 34
Joined: Tue Nov 19, 2002 1:40 pm

Post 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
CrazyJimmy
Forum Commoner
Posts: 34
Joined: Tue Nov 19, 2002 1:40 pm

Post 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];

?>
User avatar
horgh
Forum Newbie
Posts: 23
Joined: Mon Oct 21, 2002 9:50 am
Location: GER
Contact:

Post 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...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
Post Reply