Jump to content

[SOLVED] Using a PHP variable to name call another PHP variable


wintallo

Recommended Posts

Hey,

 

First, thanks for the read. Second, I need help using the contents of PHP variable ( in my case it's called $category ) to call another variable. In other words I want to call an variable called "$(whatever the contents of the $category is)_count". The category variable varies widely in its contents so I want to do it this way so I don't have to manually type in $whatever_count on every page is used this method.

 

If you don't get what I'm sayings please feel free to ask questions. Again, thanks for the read.

 

-Joel

wintallo@gmail.com

Link to comment
Share on other sites

Well, I'm kind of unsure on how to implement that knowledge in my case.

This is  what I tried, but it doesn't work.

<?php

$category = "animations";

include 'greatmovies_categorycount.php';

sort($animations);

$($category)_length = count(($category));

for ( $counter = 0; $counter <= ( $($category)_length - 1); $counter++ ) {
$title = ${$category}[$counter];
$image = ereg_replace(' ', '_', ${$category}[$counter]);
$image = ereg_replace('\:', '', $image);
$image = ereg_replace('\.', '', $image);
$image = ereg_replace('\,', '', $image);
$image = ereg_replace('\;', '', $image);
$image = ereg_replace('\?', '', $image);
$image = ereg_replace('\'', '', $image);
$image = "images/game-movie_pictures/".strtolower($image)."_pic_small.png";
$url = ereg_replace(' ', '', ${$category}[$counter]);
$url = ereg_replace('\:', '', $url);
$url = ereg_replace('\.', '', $url);
$url = ereg_replace('\,', '', $url);
$url = ereg_replace('\;', '', $url);
$url = ereg_replace('\?', '', $url);
$url = ereg_replace('\'', '', $url);
$url = "game-movie_pages/".strtolower($url).".php";
if ( $new_movie == $title ) {
	${$category . "_done"}[$counter] = '<img alt="New!" src="images/new_button.png" /><br /><a href="'.$url.'"><img alt="'.$title.'" src="'.$image.'" /></a> <a href="'.$url.'">'.$title.'</a>';
} else {
	${$category . "_done"}[$counter] = '<a href="'.$url.'"><img alt="'.$title.'" src="'.$image.'" /></a> <a href="'.$url.'">'.$title.'</a>';
}
}

echo "if this doesn't display then the codes broken!";

?>

 

Thanks for the replies too!

Link to comment
Share on other sites

Ok, I tried that and it still doesn't work!

 

This is my revised (non-functional  >:() code.

<?php

$category = "animations";

include 'greatmovies_categorycount.php';

sort($animations);

$($category)_length = count(($category));

for ( $counter = 0; $counter <= ( $($category)_length - 1); $counter++ ) {
$title = $$category[$counter];
$image = ereg_replace(' ', '_', $$category[$counter]);
        ...
$image = "images/game-movie_pictures/".strtolower($image)."_pic_small.png";
$url = ereg_replace(' ', '', $$category[$counter]);
        ...
$url = "game-movie_pages/".strtolower($url).".php";
if ( $new_movie == $title ) {
	${$category . "_done"}[$counter] = '<img alt="New!" src="images/new_button.png" /><br /><a href="'.$url.'"><img alt="'.$title.'" src="'.$image.'" /></a> <a href="'.$url.'">'.$title.'</a>';
} else {
	${$category . "_done"}[$counter] = '<a href="'.$url.'"><img alt="'.$title.'" src="'.$image.'" /></a> <a href="'.$url.'">'.$title.'</a>';
}
}

echo "if this doesn't display then the codes broken!";

?>

Link to comment
Share on other sites

Crayon Violent:

 

First, yes

include 'greatmovies_categorycount.php';

is where $animations is being set.

 

It's being set to

$animations = array("Animator vs. Animation","Black Dude Falling","Over the Rainbow","United Airlines: Dragon","Motion");

 

Second, when it test it out, it spits out a blank page, when it should output "if this doesn't display then the codes broken!" because off the third to last line in my code.

 

Corbin:

 

Yes $category is declared correctly, (3rd line of my code)

 

<?php

$category = "animations"; // right here

include 'greatmovies_categorycount.php';

 

And what I said to Crayon Violent explains that $animations is set correctly

Link to comment
Share on other sites

currently, this is what my code is

 

<?php

$category = "animations";

include 'greatmovies_categorycount.php';

sort($animations);

$($category)_length = count(($category));

for ( $counter = 0; $counter <= ( $($category)_length - 1); $counter++ ) {
$title = ${$category}[$counter];
$image = ereg_replace(' ', '_', ${$category}[$counter]);
        // stuff
$image = "images/game-movie_pictures/".strtolower($image)."_pic_small.png";
$url = ereg_replace(' ', '', ${$category}[$counter]);
        // stuff
$url = "game-movie_pages/".strtolower($url).".php";
if ( $new_movie == $title ) {
	${$category . "_done"}[$counter] = '<img alt="New!" src="images/new_button.png" /><br /><a href="'.$url.'"><img alt="'.$title.'" src="'.$image.'" /></a> <a href="'.$url.'">'.$title.'</a>';
} else {
	${$category . "_done"}[$counter] = '<a href="'.$url.'"><img alt="'.$title.'" src="'.$image.'" /></a> <a href="'.$url.'">'.$title.'</a>';
}
}

echo "if this doesn't display then the codes broken!";

?>

Thanks for all of you guy's help so far!

Link to comment
Share on other sites

Yah, I though of that, but the problem is, is that I need the multiple length variables co-existing. I'll give the code for both greatmovies_categorycount.php and greatmovies_include.php (the code I been revising).

 

greatmovies_categorycount.php

<?php

$animations = array("Animator vs. Animation","Black Dude Falling","Over the Rainbow","United Airlines: Dragon","Motion");
$clever = array("Banned Xbox 360 Commercial","Instant Kiwi Exam","What Old People Do For Fun");
$first_rate_films = array("Big Ad","Christmas Lights Show","Honda Rube Goldberg Commercial","Kiwi","Ryan vs Dorkman","Gnarls Barkley CRAZY");
$heart_stoppers = array("Ghostly Car","Pumpkin Carve");
$pure_irony = array("Ameriquest Cat Commerical","Antidote for Workaholics","Car Bait","German Coast Guard","Ice Scraping","Oh, Mama","Wow Satellite Dish","Acupuncture Insurance Commercial","Smoking Kills");
$parodies = array("Got Milk?","iPod Flea","Numa Numa Man","Small Ad","They're Taking the Hobbits to Isengard","White and Nerdy");
$short_clips = array("Blonde Antelope","Hamster Wheel Gone Wrong","How to Park a Dirt Bike","Ice Fishing","Pool Jump","Road Rage Granny");
$self_produced = array("The Random Adventures of Aaron","Three for All, and None for Us");
$slapstick = array("Cubicle War","Funny Toyota Spot","Horse and Sleigh","Man vs. Bear");
$super_bowl = array("FedEx: Stick","Sierra Mist","Vault: Scarecrow");
$weird = array("Nintendo 64 Kid Goes Crazy","Indian Rollerskater");
$wicked_skills = array("Matrix Ping Pong","Ok Go","Nintendo Accapella","Peugeot Style","Robot Dance","Pepsi Super People");

$new_movie = "Motion";

$self_produced_count = count($self_produced);
$animations_count = count($animations);
$clever_count = count($clever);
$first_rate_films_count	= count($first_rate_films);
$heart_stoppers_count = count($heart_stoppers);
$parodies_count	= count($pure_irony);
$pure_irony_count = count($parodies);
$short_clips_count = count($short_clips);
$slapstick_count = count($slapstick);
$super_bowl_count = count($super_bowl);
$weird_count = count($weird);
$wicked_skills_count = count($wicked_skills);

//$new_category = "self_produced";
$new_category = "animations";
//$new_category = "clever";
//$new_category = "first_rate_films";
//$new_category = "heart_stoppers";
//$new_category = "parodies";
//$new_category = "pure_irony";
//$new_category = "short_clips";
//$new_category = "slapstick";
//$new_category = "super_bowl";
//$new_category = "weird";
//$new_category = "wicked_skills";

?>

 

greatmovies_include.php

<?php

$category = "animations";

include 'greatmovies_categorycount.php';

sort($animations);

for ( $counter = 0; $counter <= ( $($category)_count - 1); $counter++ ) {
$title = ${$category}[$counter];
$image = ereg_replace(' ', '_', ${$category}[$counter]);
$image = ereg_replace('\:', '', $image);
$image = ereg_replace('\.', '', $image);
$image = ereg_replace('\,', '', $image);
$image = ereg_replace('\;', '', $image);
$image = ereg_replace('\?', '', $image);
$image = ereg_replace('\'', '', $image);
$image = "images/game-movie_pictures/".strtolower($image)."_pic_small.png";
$url = ereg_replace(' ', '', ${$category}[$counter]);
$url = ereg_replace('\:', '', $url);
$url = ereg_replace('\.', '', $url);
$url = ereg_replace('\,', '', $url);
$url = ereg_replace('\;', '', $url);
$url = ereg_replace('\?', '', $url);
$url = ereg_replace('\'', '', $url);
$url = "game-movie_pages/".strtolower($url).".php";
if ( $new_movie == $title ) {
	${$category . "_done"}[$counter] = '<img alt="New!" src="images/new_button.png" /><br /><a href="'.$url.'"><img alt="'.$title.'" src="'.$image.'" /></a> <a href="'.$url.'">'.$title.'</a>';
} else {
	${$category . "_done"}[$counter] = '<a href="'.$url.'"><img alt="'.$title.'" src="'.$image.'" /></a> <a href="'.$url.'">'.$title.'</a>';
}
}

echo "if this doesn't display then the codes broken!";

?>

Link to comment
Share on other sites

Ok the success message comes up but I added a bit of code to see if the generated html doesn't come up. When I look in the source of the generated page, the only this that was there was "if this doesn't display then the codes broken!" Not any of the genrated code

 

here's my code

<?php

$category = "animations";

include 'greatmovies_categorycount.php';

sort($animations);

$length = $category.'_count';

for ( $counter = 0; $counter <= ( $length - 1); $counter++ ) {
$title = ${$category}[$counter];
$image = ereg_replace(' ', '_', ${$category}[$counter]);
$image = ereg_replace('\:', '', $image);
$image = ereg_replace('\.', '', $image);
$image = ereg_replace('\,', '', $image);
$image = ereg_replace('\;', '', $image);
$image = ereg_replace('\?', '', $image);
$image = ereg_replace('\'', '', $image);
$image = "images/game-movie_pictures/".strtolower($image)."_pic_small.png";
$url = ereg_replace(' ', '', ${$category}[$counter]);
$url = ereg_replace('\:', '', $url);
$url = ereg_replace('\.', '', $url);
$url = ereg_replace('\,', '', $url);
$url = ereg_replace('\;', '', $url);
$url = ereg_replace('\?', '', $url);
$url = ereg_replace('\'', '', $url);
$url = "game-movie_pages/".strtolower($url).".php";
if ( $new_movie == $title ) {
	${$category . "_done"}[$counter] = '<img alt="New!" src="images/new_button.png" /><br /><a href="'.$url.'"><img alt="'.$title.'" src="'.$image.'" /></a> <a href="'.$url.'">'.$title.'</a>';
} else {
	${$category . "_done"}[$counter] = '<a href="'.$url.'"><img alt="'.$title.'" src="'.$image.'" /></a> <a href="'.$url.'">'.$title.'</a>';
}
echo ${$category . "_done"}[$counter];
echo "<p> </p>";
}

echo "if this doesn't display then the codes broken!";

?>

Link to comment
Share on other sites

Hip Hip Hooray! I found the problem. BUt thanks for all you help.

For future notice, this is my code.

<?php

include 'greatmovies_categorycount.php';

sort($animations);

for ( $counter = 0; $counter <= ( ${ $category . '_count' } - 1 ); $counter++ ) {
$title = ${$category}[$counter];
$image = ereg_replace(' ', '_', ${$category}[$counter]);
$image = ereg_replace('\:', '', $image);
$image = ereg_replace('\.', '', $image);
$image = ereg_replace('\,', '', $image);
$image = ereg_replace('\;', '', $image);
$image = ereg_replace('\?', '', $image);
$image = ereg_replace('\'', '', $image);
$image = "images/game-movie_pictures/".strtolower($image)."_pic_small.png";
$url = ereg_replace(' ', '', ${$category}[$counter]);
$url = ereg_replace('\:', '', $url);
$url = ereg_replace('\.', '', $url);
$url = ereg_replace('\,', '', $url);
$url = ereg_replace('\;', '', $url);
$url = ereg_replace('\?', '', $url);
$url = ereg_replace('\'', '', $url);
$url = "game-movie_pages/".strtolower($url).".php";
if ( $new_movie == $title ) {
	${$category . "_done"}[$counter] = '<img alt="New!" src="images/new_button.png" /><br /><a href="'.$url.'"><img alt="'.$title.'" src="'.$image.'" /></a> <a href="'.$url.'">'.$title.'</a>';
} else {
	${$category . "_done"}[$counter] = '<a href="'.$url.'"><img alt="'.$title.'" src="'.$image.'" /></a> <a href="'.$url.'">'.$title.'</a>';
}
echo ${$category . "_done"}[$counter];
echo "<p> </p>";
}

echo "if this doesn't display then the codes broken!";

?>

Link to comment
Share on other sites

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.