megajosh2
08-23-2006, 01:36 AM
My comic script won't work.
<!-- Comment xOMG!!
<?php
/* $_COMIC[""] is just a magic quote.
I'll call it "a fake array". lol */
$comic_id = "1";
$_COMIC["image"] = '"comic'. $comic_id .'.png"';
/* Can a magic quote equal a variable?
Apparently...
Now what else?
Oh yes, the comic title*/
$comic_title = "Nothing";
$_COMIC["name"] = "Comic #". $comic_id ." ". $comic_title ."- Megaman Generations";
function comic()
{
if ($comic_id = "1")
{$comic_title = "- The Beggining ";}
};
comic();
?> -->
<table>
<tr class="header"><td><center><b><?=$_COMIC["name"]?></b></center></td></tr>
</table>
$comic_title won't change!! WHYYYYYY!!!! No, really.
Fumigator
08-23-2006, 02:11 AM
It did change, you're just not displaying the change. You're displaying $_COMIC["name"], which is put together before you call the function comic(). Besides which, the scope of your variable $comic_title inside that function makes it a different variable than the $comic_title outside the function, so anything you do to $comic_title inside the function won't have any effect on the variable $comic_title outside the function. You can use "global $comic_title;" at the beginning of the function to solve that, or better yet return $comic_title.
And finally,
if ($comic_id = "1")
Is not checking to see if $comic_id is equal to "1"; it is assigning "1" to $comic_id, which returns "true" every time, which means that "if" statement will always evaluate to "true". You need to do use double equal signs, if ($comic_id == "1"), to see if $comic_id is equal to "1".
megajosh2
08-23-2006, 02:56 AM
Oh, thank you so much! I'm sooo happy!!
megajosh2
08-23-2006, 09:17 PM
It still doesn't work. I then edited it for I dunno how long so I just tried to change it a little.
<?php
//this displays the comic and title
$_COMIC["id"] = "1";
$_COMIC["image"] = 'comics/comic'. $_COMIC["id"] .'.png';
$_COMIC["title"] = "php script error: comic title not working.";
$_COMIC["name"] = "Comic #". $_COMIC["id"] ." ". $_COMIC["title"] ."- Megaman Generations";
if ($_COMIC["id"] == "1")
{$_COMIC["title"] = "The Beggining";}
?>
<table>
<tr class="header"><td><center><b><?=$_COMIC["name"]?></b></center></td></tr>
<tr bgcolor="#6495ED"><td><center><img src=<?=$_COMIC["image"]?> alt="No comic"></center></td></tr>
</table>
Still not changing. Anything I tell to change in the "if" thingy won't change. It it becasue there's no function? When I do that, I move all of the variables into it. Then nothing show up. I put function(); at the end of the script too.
what are you expecting to happen? to explain line by line:
$_COMIC["id"] = "1";
you set the id to be 1.
$_COMIC["image"] = 'comics/comic'. $_COMIC["id"] .'.png';
image now equals 'comics/comic1.png'
$_COMIC["title"] = "php script error: comic title not working.";
setting the title
$_COMIC["name"] = "Comic #". $_COMIC["id"] ." ". $_COMIC["title"] ."- Megaman Generations";
name now equals 'Comic #1 php script error: comic title not working'
if ($_COMIC["id"] == "1")
{$_COMIC["title"] = "The Beggining";}
The id does equal 1, so title gets set to be 'The beggining'[sic]
...
<tr class="header"><td><center><b><?=$_COMIC["name"]?></b></center></td></tr>
here, name (which is still 'Comic #1 php script error: comic title not working' as it hasn't been changed) is echo'd inside a table cell
<tr bgcolor="#6495ED"><td><center><img src=<?=$_COMIC["image"]?> alt="No comic"></center></td></tr>
</table>
and then 'image' ( 'comics/comic1.png') gets echo'd as the source of your img tag.
right, as I was typing I think I worked out your problem. You're thinking that the reference to title used when setting name will get updated when you change title. It won't. Concatenation of strings results in a new string, it is a literal value, and change to variables that were used in its composition will have no effect. (emphasis added to appropriate bits)
megajosh2
08-23-2006, 09:39 PM
So I assemble $_COMIC["name"] before the "if" function or should I change it to $comic_name? Will the variables used to assemble the title change if it isn't magic?
you probably want:
$_COMIC["id"] = "1";
$_COMIC["image"] = 'comics/comic'. $_COMIC["id"] .'.png';
$_COMIC["title"] = "php script error: comic title not working.";
if ($_COMIC["id"] == "1")
{$_COMIC["title"] = "The Beggining";}
$_COMIC["name"] = "Comic #". $_COMIC["id"] ." ". $_COMIC["title"] ."- Megaman Generations";
megajosh2
08-23-2006, 10:03 PM
Ahh. Okay. I'll tell back how that works. (I learned an important lesson :))
Edit: IT worked!! Your awesome!!
megajosh2
08-24-2006, 02:13 AM
2 questions.
1. Can onClick be used for a php function?
2. Can you send a form to "get" if the variable is a magic variable?
megajosh2
08-24-2006, 04:34 AM
It looks like typing ?comic_id=# doesn't change $comic_id to # when # is a number.
marek_mar
08-24-2006, 04:47 AM
1. No
2. Your $_COMIC variable isn't magical.
2. Your $_COMIC variable isn't magical.
not sure why, but I laughed at that :)
megajosh2
08-24-2006, 08:46 PM
Can someone help me here? adding ?comic_id=# to the url doesn't change $comic_id. Does anyone know why? Do I need a $_GET? What I mean is http://mmgenerations.awardspace.com/comics.php?comic_id=# won't change $comic_id to #.
megajosh2
08-24-2006, 10:02 PM
Never mind. I solved it.
$comic_id = $_GET["comic_id"]
I'm a n00blet forgive me. ;)