473,379 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Question about 'sizeof'

SM
Hello,
I have another simple question about an array in PHP and a variable in
PHP.

This is the array:

$thumbs_cat_1 = array(
'wine',
'cheese',
'ice',
'bread'
);
$thumbs_cat_2 = array(
'hello',
'goodbye'
);
This will work:
$item_total = sizeof($thumbs_cat_1);
output: 4

$item_total = sizeof($thumbs_cat_2);
output: 2
Now, i want to replace the parameter in 'sizeof' by a variable

$thumb = 1;
$item = '$thumbs_cat_' . $thumb; //this outputs a string:
$thumbs_cat_1

$item_total = sizeof($item);
output: Nothing!

How do i pass a variable to the 'sizeof' function?

Thanks
Marco

Jun 2 '08 #1
15 1499
SM wrote:
Hello,
I have another simple question about an array in PHP and a variable in
PHP.

This is the array:

$thumbs_cat_1 = array(
'wine',
'cheese',
'ice',
'bread'
);
$thumbs_cat_2 = array(
'hello',
'goodbye'
);
This will work:
$item_total = sizeof($thumbs_cat_1);
output: 4

$item_total = sizeof($thumbs_cat_2);
output: 2
Now, i want to replace the parameter in 'sizeof' by a variable

$thumb = 1;
$item = '$thumbs_cat_' . $thumb; //this outputs a string:
$thumbs_cat_1

$item_total = sizeof($item);
output: Nothing!

How do i pass a variable to the 'sizeof' function?
You should use variable variable:
$item_total = sizeof($$item);

Mark double $$
This is true in any situation, not only with sizeof

best regards
Piotr N
Jun 2 '08 #2
On May 8, 11:15 am, SM <servandomont...@gmail.comwrote:
Hello,
I have another simple question about an array in PHP and a variable in
PHP.

This is the array:

$thumbs_cat_1 = array(
'wine',
'cheese',
'ice',
'bread'
);

$thumbs_cat_2 = array(
'hello',
'goodbye'
);

This will work:
$item_total = sizeof($thumbs_cat_1);
output: 4

$item_total = sizeof($thumbs_cat_2);
output: 2

Now, i want to replace the parameter in 'sizeof' by a variable

$thumb = 1;
$item = '$thumbs_cat_' . $thumb; //this outputs a string:
$thumbs_cat_1

$item_total = sizeof($item);
output: Nothing!

How do i pass a variable to the 'sizeof' function?

Thanks
Marco
Marco,

You should be getting a return of 1 (integer) for a string passed to
the count() function. (sizeof is an alias of count)

To get the length of a string, you would traditionally use
strlen($item), which would return the length of a string.

Regards,

Steve
Jun 2 '08 #3
On May 8, 11:36 am, ELINTPimp <smsi...@gmail.comwrote:
On May 8, 11:15 am, SM <servandomont...@gmail.comwrote:
Hello,
I have another simple question about an array in PHP and a variable in
PHP.
This is the array:
$thumbs_cat_1 = array(
'wine',
'cheese',
'ice',
'bread'
);
$thumbs_cat_2 = array(
'hello',
'goodbye'
);
This will work:
$item_total = sizeof($thumbs_cat_1);
output: 4
$item_total = sizeof($thumbs_cat_2);
output: 2
Now, i want to replace the parameter in 'sizeof' by a variable
$thumb = 1;
$item = '$thumbs_cat_' . $thumb; //this outputs a string:
$thumbs_cat_1
$item_total = sizeof($item);
output: Nothing!
How do i pass a variable to the 'sizeof' function?
Thanks
Marco

Marco,

You should be getting a return of 1 (integer) for a string passed to
the count() function. (sizeof is an alias of count)

To get the length of a string, you would traditionally use
strlen($item), which would return the length of a string.

Regards,

Steve
Ahhh, now i understand after reading Pitor's reply. Yes, he is
correct, I read the question differently.

Steve
Jun 2 '08 #4
>$thumb = 1;
>$item = '$thumbs_cat_' . $thumb; //this outputs a string:
$thumbs_cat_1

$item_total = sizeof($item);
output: Nothing!

How do i pass a variable to the 'sizeof' function?

You should use variable variable:
$item_total = sizeof($$item);

Mark double $$
This is true in any situation, not only with sizeof
Forgot about one small detail:
$item = 'thumbs_cat_' . $thumb; //this outputs a string:
---------^ loose the '$';

best regards
Piotr N
Jun 2 '08 #5
SM
On May 8, 11:36 am, ELINTPimp <smsi...@gmail.comwrote:
On May 8, 11:15 am, SM <servandomont...@gmail.comwrote:
Hello,
I have another simple question about an array in PHP and a variable in
PHP.
This is the array:
$thumbs_cat_1 = array(
'wine',
'cheese',
'ice',
'bread'
);
$thumbs_cat_2 = array(
'hello',
'goodbye'
);
This will work:
$item_total = sizeof($thumbs_cat_1);
output: 4
$item_total = sizeof($thumbs_cat_2);
output: 2
Now, i want to replace the parameter in 'sizeof' by a variable
$thumb = 1;
$item = '$thumbs_cat_' . $thumb; //this outputs a string:
$thumbs_cat_1
$item_total = sizeof($item);
output: Nothing!
How do i pass a variable to the 'sizeof' function?
Thanks
Marco

Marco,

You should be getting a return of 1 (integer) for a string passed to
the count() function. (sizeof is an alias of count)

To get the length of a string, you would traditionally use
strlen($item), which would return the length of a string.

Regards,

Steve
I don't need the length of the string , just the actual variable. But
i did like the fact that sizeof is an alias of count. So i will use
count instead.
I didn't know about the $$ tip. Wow! so easy. Thanks, It Works!

Heres the final code:

$thumb = 1;
$item = '$thumbs_cat_' . $thumb;
$item_total = count($item);

Thanks again
Marco
Jun 2 '08 #6
SM
On May 8, 11:42 am, Piotr <s...@poczta.onet.plwrote:
$thumb = 1;
$item = '$thumbs_cat_' . $thumb; //this outputs a string:
$thumbs_cat_1
$item_total = sizeof($item);
output: Nothing!
How do i pass a variable to the 'sizeof' function?
You should use variable variable:
$item_total = sizeof($$item);
Mark double $$
This is true in any situation, not only with sizeof

Forgot about one small detail:
$item = 'thumbs_cat_' . $thumb; //this outputs a string:
---------^ loose the '$';

best regards
Piotr N
I don't need the length of the string , just the actual variable. But
i did like the fact that sizeof is an alias of count. So i will use
count instead.
I didn't know about the $$ tip. Wow! so easy. Thanks, It Works!

Heres the final code:

$thumb = 1;
$item = 'thumbs_cat_' . $thumb;
$item_total = count($item);

Thanks again
Marco
Jun 2 '08 #7
SM
On May 8, 11:42 am, Piotr <s...@poczta.onet.plwrote:
$thumb = 1;
$item = '$thumbs_cat_' . $thumb; //this outputs a string:
$thumbs_cat_1
$item_total = sizeof($item);
output: Nothing!
How do i pass a variable to the 'sizeof' function?
You should use variable variable:
$item_total = sizeof($$item);
Mark double $$
This is true in any situation, not only with sizeof

Forgot about one small detail:
$item = 'thumbs_cat_' . $thumb; //this outputs a string:
---------^ loose the '$';

best regards
Piotr N
I don't need the length of the string , just the actual variable. But
i did like the fact that sizeof is an alias of count. So i will use
count instead.
I didn't know about the $$ tip. Wow! so easy. Thanks, It Works!

Heres the final code:

$thumb = 1;
$item = 'thumbs_cat_' . $thumb;
$item_total = count($$item);

Thanks again
Marco
Jun 2 '08 #8
SM
On May 8, 11:50 am, SM <servandomont...@gmail.comwrote:
On May 8, 11:42 am, Piotr <s...@poczta.onet.plwrote:
>$thumb = 1;
>$item = '$thumbs_cat_' . $thumb; //this outputs a string:
>$thumbs_cat_1
>$item_total = sizeof($item);
>output: Nothing!
>How do i pass a variable to the 'sizeof' function?
You should use variable variable:
$item_total = sizeof($$item);
Mark double $$
This is true in any situation, not only with sizeof
Forgot about one small detail:
$item = 'thumbs_cat_' . $thumb; //this outputs a string:
---------^ loose the '$';
best regards
Piotr N

I don't need the length of the string , just the actual variable. But
i did like the fact that sizeof is an alias of count. So i will use
count instead.
I didn't know about the $$ tip. Wow! so easy. Thanks, It Works!

Heres the final code:

$thumb = 1;
$item = 'thumbs_cat_' . $thumb;
$item_total = count($$item);

Thanks again
Marco
Maybe i should open a new post for this one, but here it goes:

I'm stock! When i want to reference that variable in a echo statment,
it does't work. Any suggestions?
Thanks

<li><a href="<?php echo $SERVER['PHP_SELF']; ?>?cat=<?php echo
$category_this; ?>"><img src="images/<?php echo($$item[$i]);?>"
alt="" /></a></li>
Jun 2 '08 #9
SM wrote:
On May 8, 11:50 am, SM <servandomont...@gmail.comwrote:
>On May 8, 11:42 am, Piotr <s...@poczta.onet.plwrote:
>>>>$thumb = 1;
$item = '$thumbs_cat_' . $thumb; //this outputs a string:
$thumbs_cat_1
$item_total = sizeof($item);
output: Nothing!
How do i pass a variable to the 'sizeof' function?
You should use variable variable:
$item_total = sizeof($$item);
Mark double $$
This is true in any situation, not only with sizeof
Forgot about one small detail:
$item = 'thumbs_cat_' . $thumb; //this outputs a string:
---------^ loose the '$';
best regards
Piotr N
I don't need the length of the string , just the actual variable. But
i did like the fact that sizeof is an alias of count. So i will use
count instead.
I didn't know about the $$ tip. Wow! so easy. Thanks, It Works!

Heres the final code:

$thumb = 1;
$item = 'thumbs_cat_' . $thumb;
$item_total = count($$item);

Thanks again
Marco

Maybe i should open a new post for this one, but here it goes:

I'm stock! When i want to reference that variable in a echo statment,
it does't work. Any suggestions?
Thanks

<li><a href="<?php echo $SERVER['PHP_SELF']; ?>?cat=<?php echo
$category_this; ?>"><img src="images/<?php echo($$item[$i]);?>"
alt="" /></a></li>
:)
It should look like this. Mark the { and }
echo ${$item[$i]};

As a side note, is this really best way to access needed data this way ?
Cant you keep original values in the array instead of the variable names ?

best regards
Piotr N
Jun 2 '08 #10
SM
On May 8, 12:31 pm, Piotr <s...@poczta.onet.plwrote:
SM wrote:
On May 8, 11:50 am, SM <servandomont...@gmail.comwrote:
On May 8, 11:42 am, Piotr <s...@poczta.onet.plwrote:
>>>$thumb = 1;
$item = '$thumbs_cat_' . $thumb; //this outputs a string:
$thumbs_cat_1
$item_total = sizeof($item);
output: Nothing!
How do i pass a variable to the 'sizeof' function?
You should use variable variable:
$item_total = sizeof($$item);
Mark double $$
This is true in any situation, not only with sizeof
Forgot about one small detail:
$item = 'thumbs_cat_' . $thumb; //this outputs a string:
---------^ loose the '$';
best regards
Piotr N
I don't need the length of the string , just the actual variable. But
i did like the fact that sizeof is an alias of count. So i will use
count instead.
I didn't know about the $$ tip. Wow! so easy. Thanks, It Works!
Heres the final code:
$thumb = 1;
$item = 'thumbs_cat_' . $thumb;
$item_total = count($$item);
Thanks again
Marco
Maybe i should open a new post for this one, but here it goes:
I'm stock! When i want to reference that variable in a echo statment,
it does't work. Any suggestions?
Thanks
<li><a href="<?php echo $SERVER['PHP_SELF']; ?>?cat=<?php echo
$category_this; ?>"><img src="images/<?php echo($$item[$i]);?>"
alt="" /></a></li>

:)
It should look like this. Mark the { and }
echo ${$item[$i]};

As a side note, is this really best way to access needed data this way ?
Cant you keep original values in the array instead of the variable names ?

best regards
Piotr N
I've tried it, It doesn't work. When i look at the source code, i see
empty an empty string:
<img src="images/" alt="" />

Here's what im trying to do:
-From a list, the user selects the type of discography (either
original, compilation, tributes, other)
-Each type of discography is associated with an array that contains
the name of the cd cover.
-Then i show a thumbnail (using a loop) of all the cd covers depending
on the array.

my arrays look like this
$thumbs_cat_1 = array('moonlight.jpg'', shadow.jpg', 'luna');
$thumbs_cat_2 = array(...);
$thumbs_cat_3 = array(...);
$thumbs_cat_4 = array(...);

the thumbs look like this:
for($i=0; ...){
....<img src="images/discography/thumbs/<?php echo $thumbs_cat_1[$i]; ?
>" alt="" />
}

All of this , works for the first array.
That's why i have a variable that contains the selection from the
user:
$thumb = $_GET['cat'];
$item = 'thumbs_cat_' . $thumb;
$item_total = count($$item);
.... and so on

That's why i need to create images according to the user selection

Any ideas what's wrong with the code?
Thanks
Marco
Jun 2 '08 #11
SM
On May 8, 12:31 pm, Piotr <s...@poczta.onet.plwrote:
SM wrote:
On May 8, 11:50 am, SM <servandomont...@gmail.comwrote:
On May 8, 11:42 am, Piotr <s...@poczta.onet.plwrote:
>>>$thumb = 1;
$item = '$thumbs_cat_' . $thumb; //this outputs a string:
$thumbs_cat_1
$item_total = sizeof($item);
output: Nothing!
How do i pass a variable to the 'sizeof' function?
You should use variable variable:
$item_total = sizeof($$item);
Mark double $$
This is true in any situation, not only with sizeof
Forgot about one small detail:
$item = 'thumbs_cat_' . $thumb; //this outputs a string:
---------^ loose the '$';
best regards
Piotr N
I don't need the length of the string , just the actual variable. But
i did like the fact that sizeof is an alias of count. So i will use
count instead.
I didn't know about the $$ tip. Wow! so easy. Thanks, It Works!
Heres the final code:
$thumb = 1;
$item = 'thumbs_cat_' . $thumb;
$item_total = count($$item);
Thanks again
Marco
Maybe i should open a new post for this one, but here it goes:
I'm stock! When i want to reference that variable in a echo statment,
it does't work. Any suggestions?
Thanks
<li><a href="<?php echo $SERVER['PHP_SELF']; ?>?cat=<?php echo
$category_this; ?>"><img src="images/<?php echo($$item[$i]);?>"
alt="" /></a></li>

:)
It should look like this. Mark the { and }
echo ${$item[$i]};

As a side note, is this really best way to access needed data this way ?
Cant you keep original values in the array instead of the variable names ?

best regards
Piotr N
I've tried it, It doesn't work. When i look at the source code, i see
empty an empty string:
<img src="images/" alt="" />

Here's what im trying to do:
-From a list, the user selects the category of discography (either
original, compilation, tributes, other)
-Each type of discography is associated with an array that contains
the name of the cd covers.
-Then i show a thumbnail (using a loop) of all the cd covers.

my arrays look like this (one for each category of discography)
$thumbs_cat_1 = array('moonlight.jpg'', shadow.jpg', 'luna.jpg');
$thumbs_cat_2 = array(...);
$thumbs_cat_3 = array(...);
$thumbs_cat_4 = array(...);

Then i create the thumbs like this:
for($i=0; ...){
....<img src="images/discography/thumbs/<?php echo $thumbs_cat_1[$i]; ?
>" alt="" />
}

All of this works perfectly for the first array.
That's why, i think, i need a variable that contains the selection
from the user (the category of discography) so i can select values
from the correct array.
I do it like this:

$cat = $_GET['cat']; //output: 1
$sel_array = 'thumbs_cat_' . $cat;//create the string: thumbs_cat_1


Then i create the thumbs like this:
for($i=0; ...){
....<img src="images/discography/thumbs/<?php echo ${$sel_array[$i]}; ?
>" alt="" />
}
.... and so on

You think my code makes sense? Maybe i'm doing it wrong.
The last part is were im stock...
Any ideas what's wrong?
Thanks again
Marco
Jun 2 '08 #12
SM
On May 8, 12:31 pm, Piotr <s...@poczta.onet.plwrote:
SM wrote:
On May 8, 11:50 am, SM <servandomont...@gmail.comwrote:
On May 8, 11:42 am, Piotr <s...@poczta.onet.plwrote:
>>>$thumb = 1;
$item = '$thumbs_cat_' . $thumb; //this outputs a string:
$thumbs_cat_1
$item_total = sizeof($item);
output: Nothing!
How do i pass a variable to the 'sizeof' function?
You should use variable variable:
$item_total = sizeof($$item);
Mark double $$
This is true in any situation, not only with sizeof
Forgot about one small detail:
$item = 'thumbs_cat_' . $thumb; //this outputs a string:
---------^ loose the '$';
best regards
Piotr N
I don't need the length of the string , just the actual variable. But
i did like the fact that sizeof is an alias of count. So i will use
count instead.
I didn't know about the $$ tip. Wow! so easy. Thanks, It Works!
Heres the final code:
$thumb = 1;
$item = 'thumbs_cat_' . $thumb;
$item_total = count($$item);
Thanks again
Marco
Maybe i should open a new post for this one, but here it goes:
I'm stock! When i want to reference that variable in a echo statment,
it does't work. Any suggestions?
Thanks
<li><a href="<?php echo $SERVER['PHP_SELF']; ?>?cat=<?php echo
$category_this; ?>"><img src="images/<?php echo($$item[$i]);?>"
alt="" /></a></li>

:)
It should look like this. Mark the { and }
echo ${$item[$i]};

As a side note, is this really best way to access needed data this way ?
Cant you keep original values in the array instead of the variable names ?

best regards
Piotr N
I've tried it, It doesn't work. When i look at the source code, i see
an empty string:
<img src="images/" alt="" />

Here's what im trying to do:
-From a list, the user selects the category of discography (either
original, compilation, tributes, other)
-Each type of discography is associated with an array that contains
the name of the cd covers.
-Then i show a thumbnail (using a loop) of all the cd covers.

my arrays look like this (one for each category of discography)
$thumbs_cat_1 = array('moonlight.jpg'', shadow.jpg', 'luna.jpg');
$thumbs_cat_2 = array(...);
$thumbs_cat_3 = array(...);
$thumbs_cat_4 = array(...);

Then i create the thumbs like this:
for($i=0; ...){
....<img src="images/discography/thumbs/<?php echo $thumbs_cat_1[$i]; ?
>" alt="" />
}

All of this works perfectly for the first array.
That's why, i think, i need a variable that contains the selection
from the user (the category of discography) so i can select values
from the correct array.
I do it like this:

$cat = $_GET['cat']; //output: 1
$sel_array = 'thumbs_cat_' . $cat;//create the string: thumbs_cat_1


Then i create the thumbs like this:
for($i=0; ...){
....<img src="images/discography/thumbs/<?php echo ${$sel_array[$i]}; ?
>" alt="" />
}
.... and so on

You think my code makes sense? Maybe i'm doing it wrong.
The last part is were im stock...
Any ideas what's wrong?
Thanks again
Marco
Jun 2 '08 #13
SM wrote:
On May 8, 12:31 pm, Piotr <s...@poczta.onet.plwrote:
>SM wrote:
>>On May 8, 11:50 am, SM <servandomont...@gmail.comwrote:
On May 8, 11:42 am, Piotr <s...@poczta.onet.plwrote:
>>$thumb = 1;
>>$item = '$thumbs_cat_' . $thumb; //this outputs a string:
>>$thumbs_cat_1
>>$item_total = sizeof($item);
>>output: Nothing!
>>How do i pass a variable to the 'sizeof' function?
>You should use variable variable:
>$item_total = sizeof($$item);
>Mark double $$
>This is true in any situation, not only with sizeof
Forgot about one small detail:
$item = 'thumbs_cat_' . $thumb; //this outputs a string:
---------^ loose the '$';
best regards
Piotr N
I don't need the length of the string , just the actual variable. But
i did like the fact that sizeof is an alias of count. So i will use
count instead.
I didn't know about the $$ tip. Wow! so easy. Thanks, It Works!
Heres the final code:
$thumb = 1;
$item = 'thumbs_cat_' . $thumb;
$item_total = count($$item);
Thanks again
Marco
Maybe i should open a new post for this one, but here it goes:
I'm stock! When i want to reference that variable in a echo statment,
it does't work. Any suggestions?
Thanks
<li><a href="<?php echo $SERVER['PHP_SELF']; ?>?cat=<?php echo
$category_this; ?>"><img src="images/<?php echo($$item[$i]);?>"
alt="" /></a></li>
:)
It should look like this. Mark the { and }
echo ${$item[$i]};

As a side note, is this really best way to access needed data this way ?
Cant you keep original values in the array instead of the variable names ?

best regards
Piotr N

I've tried it, It doesn't work. When i look at the source code, i see
an empty string:
<img src="images/" alt="" />

Here's what im trying to do:
-From a list, the user selects the category of discography (either
original, compilation, tributes, other)
-Each type of discography is associated with an array that contains
the name of the cd covers.
-Then i show a thumbnail (using a loop) of all the cd covers.

my arrays look like this (one for each category of discography)
$thumbs_cat_1 = array('moonlight.jpg'', shadow.jpg', 'luna.jpg');
$thumbs_cat_2 = array(...);
$thumbs_cat_3 = array(...);
$thumbs_cat_4 = array(...);

Then i create the thumbs like this:
for($i=0; ...){
...<img src="images/discography/thumbs/<?php echo $thumbs_cat_1[$i]; ?
>" alt="" />
}

All of this works perfectly for the first array.
That's why, i think, i need a variable that contains the selection
from the user (the category of discography) so i can select values
from the correct array.
I do it like this:

$cat = $_GET['cat']; //output: 1
$sel_array = 'thumbs_cat_' . $cat;//create the string: thumbs_cat_1


Then i create the thumbs like this:
for($i=0; ...){
...<img src="images/discography/thumbs/<?php echo ${$sel_array[$i]}; ?
>" alt="" />
}
... and so on

You think my code makes sense? Maybe i'm doing it wrong.
The last part is were im stock...
Any ideas what's wrong?
Thanks again
Marco
Why go to all that trouble. Just use a two dimensional array, i.e.

$thumb_cats = array();
$thumb_cats[] = array('moonlight.jpg'', shadow.jpg', 'luna.jpg');
$thumb_cats[] = array(...);
$thumb_cats[] = array(...);
$thumb_cats[] = array(...);
$thumb_cats[] = array(...);

<img src="images/discography/thumbs/<?php echo $thumb_cats[$sel][$i];?>"
alt="" />

Much cleaner.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 2 '08 #14
SM
On May 8, 4:27 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
SM wrote:
On May 8, 12:31 pm, Piotr <s...@poczta.onet.plwrote:
SM wrote:
On May 8, 11:50 am, SM <servandomont...@gmail.comwrote:
On May 8, 11:42 am, Piotr <s...@poczta.onet.plwrote:
>$thumb = 1;
>$item = '$thumbs_cat_' . $thumb; //this outputs a string:
>$thumbs_cat_1
>$item_total = sizeof($item);
>output: Nothing!
>How do i pass a variable to the 'sizeof' function?
You should use variable variable:
$item_total = sizeof($$item);
Mark double $$
This is true in any situation, not only with sizeof
Forgot about one small detail:
$item = 'thumbs_cat_' . $thumb; //this outputs a string:
---------^ loose the '$';
best regards
Piotr N
I don't need the length of the string , just the actual variable. But
i did like the fact that sizeof is an alias of count. So i will use
count instead.
I didn't know about the $$ tip. Wow! so easy. Thanks, It Works!
Heres the final code:
$thumb = 1;
$item = 'thumbs_cat_' . $thumb;
$item_total = count($$item);
Thanks again
Marco
Maybe i should open a new post for this one, but here it goes:
I'm stock! When i want to reference that variable in a echo statment,
it does't work. Any suggestions?
Thanks
<li><a href="<?php echo $SERVER['PHP_SELF']; ?>?cat=<?php echo
$category_this; ?>"><img src="images/<?php echo($$item[$i]);?>"
alt="" /></a></li>
:)
It should look like this. Mark the { and }
echo ${$item[$i]};
As a side note, is this really best way to access needed data this way ?
Cant you keep original values in the array instead of the variable names ?
best regards
Piotr N
I've tried it, It doesn't work. When i look at the source code, i see
an empty string:
<img src="images/" alt="" />
Here's what im trying to do:
-From a list, the user selects the category of discography (either
original, compilation, tributes, other)
-Each type of discography is associated with an array that contains
the name of the cd covers.
-Then i show a thumbnail (using a loop) of all the cd covers.
my arrays look like this (one for each category of discography)
$thumbs_cat_1 = array('moonlight.jpg'', shadow.jpg', 'luna.jpg');
$thumbs_cat_2 = array(...);
$thumbs_cat_3 = array(...);
$thumbs_cat_4 = array(...);
Then i create the thumbs like this:
for($i=0; ...){
...<img src="images/discography/thumbs/<?php echo $thumbs_cat_1[$i]; ?
" alt="" />
}
All of this works perfectly for the first array.
That's why, i think, i need a variable that contains the selection
from the user (the category of discography) so i can select values
from the correct array.
I do it like this:
$cat = $_GET['cat']; //output: 1
$sel_array = 'thumbs_cat_' . $cat;//create the string: thumbs_cat_1
Then i create the thumbs like this:
for($i=0; ...){
...<img src="images/discography/thumbs/<?php echo ${$sel_array[$i]}; ?
" alt="" />
}
... and so on
You think my code makes sense? Maybe i'm doing it wrong.
The last part is were im stock...
Any ideas what's wrong?
Thanks again
Marco

Why go to all that trouble. Just use a two dimensional array, i.e.

$thumb_cats = array();
$thumb_cats[] = array('moonlight.jpg'', shadow.jpg', 'luna.jpg');
$thumb_cats[] = array(...);
$thumb_cats[] = array(...);
$thumb_cats[] = array(...);
$thumb_cats[] = array(...);

<img src="images/discography/thumbs/<?php echo $thumb_cats[$sel][$i];?>"
alt="" />

Much cleaner.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Hey Jeery! Thanks . That help a lot! Much cleaner and nicer.

Marco
Jun 2 '08 #15
SM
On May 8, 4:27 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
SM wrote:
On May 8, 12:31 pm, Piotr <s...@poczta.onet.plwrote:
SM wrote:
On May 8, 11:50 am, SM <servandomont...@gmail.comwrote:
On May 8, 11:42 am, Piotr <s...@poczta.onet.plwrote:
>$thumb = 1;
>$item = '$thumbs_cat_' . $thumb; //this outputs a string:
>$thumbs_cat_1
>$item_total = sizeof($item);
>output: Nothing!
>How do i pass a variable to the 'sizeof' function?
You should use variable variable:
$item_total = sizeof($$item);
Mark double $$
This is true in any situation, not only with sizeof
Forgot about one small detail:
$item = 'thumbs_cat_' . $thumb; //this outputs a string:
---------^ loose the '$';
best regards
Piotr N
I don't need the length of the string , just the actual variable. But
i did like the fact that sizeof is an alias of count. So i will use
count instead.
I didn't know about the $$ tip. Wow! so easy. Thanks, It Works!
Heres the final code:
$thumb = 1;
$item = 'thumbs_cat_' . $thumb;
$item_total = count($$item);
Thanks again
Marco
Maybe i should open a new post for this one, but here it goes:
I'm stock! When i want to reference that variable in a echo statment,
it does't work. Any suggestions?
Thanks
<li><a href="<?php echo $SERVER['PHP_SELF']; ?>?cat=<?php echo
$category_this; ?>"><img src="images/<?php echo($$item[$i]);?>"
alt="" /></a></li>
:)
It should look like this. Mark the { and }
echo ${$item[$i]};
As a side note, is this really best way to access needed data this way ?
Cant you keep original values in the array instead of the variable names ?
best regards
Piotr N
I've tried it, It doesn't work. When i look at the source code, i see
an empty string:
<img src="images/" alt="" />
Here's what im trying to do:
-From a list, the user selects the category of discography (either
original, compilation, tributes, other)
-Each type of discography is associated with an array that contains
the name of the cd covers.
-Then i show a thumbnail (using a loop) of all the cd covers.
my arrays look like this (one for each category of discography)
$thumbs_cat_1 = array('moonlight.jpg'', shadow.jpg', 'luna.jpg');
$thumbs_cat_2 = array(...);
$thumbs_cat_3 = array(...);
$thumbs_cat_4 = array(...);
Then i create the thumbs like this:
for($i=0; ...){
...<img src="images/discography/thumbs/<?php echo $thumbs_cat_1[$i]; ?
" alt="" />
}
All of this works perfectly for the first array.
That's why, i think, i need a variable that contains the selection
from the user (the category of discography) so i can select values
from the correct array.
I do it like this:
$cat = $_GET['cat']; //output: 1
$sel_array = 'thumbs_cat_' . $cat;//create the string: thumbs_cat_1
Then i create the thumbs like this:
for($i=0; ...){
...<img src="images/discography/thumbs/<?php echo ${$sel_array[$i]}; ?
" alt="" />
}
... and so on
You think my code makes sense? Maybe i'm doing it wrong.
The last part is were im stock...
Any ideas what's wrong?
Thanks again
Marco

Why go to all that trouble. Just use a two dimensional array, i.e.

$thumb_cats = array();
$thumb_cats[] = array('moonlight.jpg'', shadow.jpg', 'luna.jpg');
$thumb_cats[] = array(...);
$thumb_cats[] = array(...);
$thumb_cats[] = array(...);
$thumb_cats[] = array(...);

<img src="images/discography/thumbs/<?php echo $thumb_cats[$sel][$i];?>"
alt="" />

Much cleaner.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Hey Jerry! Thanks . That help a lot! Much cleaner and nicer and it
works perfectly! :)

Marco
Jun 2 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sims | last post by:
Hi, I have some small questions that have never been any problems, (for my compiler?), but always make me curious. So here goes... what does the standard sday about the 'if' statement? for...
17
by: ThazKool | last post by:
Why is it that calling sizeof on a class that contains only one char& returns the number 4? Should it have a 0 size?
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
30
by: Christopher Benson-Manica | last post by:
Given the following: char s="Hello, world\n!"; Are all the following guaranteed to produce the same output? printf( "%s", s ); fprintf( stdout, "%s", s ); fwrite( s, sizeof(char),...
35
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
42
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
25
by: Why Tea | last post by:
Thanks to those who have answered my original question. I thought I understood the answer and set out to write some code to prove my understanding. The code was written without any error checking....
19
by: lawtrevor | last post by:
I have a question regarding the use of free() based on some code I need to decipher: { struct A {<A fields>}; struct B {<A fields+ <Bfields>}; typedef struct A Aobj; typedef struct B Bobj;
7
by: linq936 | last post by:
Hi, I am puzzled on this C puzzle: How to interpret this C statement: sizeof (int) * p Is that the size of integer type multiplies p or the size of whatever p points to in integer type? I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.