mcfmullen Posted June 24, 2010 Share Posted June 24, 2010 You know, no matter how much I try, I can't get this to work... $img = 'http://www.cool.com/stuff/' . $table . '/'; The url simply shows http://www.cool.com/stuff// at output instead of http://www.cool.com/stuff/tableName/ Can anyone help me get this working? Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/ Share on other sites More sharing options...
mrMarcus Posted June 24, 2010 Share Posted June 24, 2010 $table is not set. Post the code where you are setting value to $table. Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/#findComment-1076847 Share on other sites More sharing options...
mcfmullen Posted June 24, 2010 Author Share Posted June 24, 2010 $table is set, otherwise the page wouldn't even load to being with. $table contains a given table name delivered through GET. $table is used mainly to look inside the appropriate MySQL table but also in places where the table name is used for display on the website. I know for a fact that the variable is set because the page loads the contents of the proper table. What it isn't doing is working within this variable. For example, I have a MySQL table called animal hat contains information about given animals. Where animal = goat, I have a page detailing the goat. The variable is set because I can load such a page, the page tells me that this is a goat but it isn't displaying the goat's image because it isn't working inside the img variable. Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/#findComment-1076865 Share on other sites More sharing options...
mcfmullen Posted June 24, 2010 Author Share Posted June 24, 2010 $table = $_GET['type']; $item = $_GET['item']; $img = 'http://www.cool.com/stuff/' . $table . '/'; This all using a url as such: http://www.cool.com/things.php?type=animal&item=Goat Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/#findComment-1076868 Share on other sites More sharing options...
PFMaBiSmAd Posted June 24, 2010 Share Posted June 24, 2010 In the code that sets $img = "..." . $table . "...";, $table does not have a value. Pretty simple, yes? If you want someone in a forum to help you determine why that would be so, you must post your actual code (not just a couple of lines taken out of context.) Telling us about what your code is supposed to do is pointless, because obviously it is not doing what you are stating. Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/#findComment-1076870 Share on other sites More sharing options...
KevinM1 Posted June 24, 2010 Share Posted June 24, 2010 Have you tried echoing $img? Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/#findComment-1076873 Share on other sites More sharing options...
mrMarcus Posted June 24, 2010 Share Posted June 24, 2010 $table is set, otherwise the page wouldn't even load to being with. $table contains a given table name delivered through GET. $table is used mainly to look inside the appropriate MySQL table but also in places where the table name is used for display on the website. I know for a fact that the variable is set because the page loads the contents of the proper table. What it isn't doing is working within this variable. For example, I have a MySQL table called animal hat contains information about given animals. Where animal = goat, I have a page detailing the goat. The variable is set because I can load such a page, the page tells me that this is a goat but it isn't displaying the goat's image because it isn't working inside the img variable. You must be unsetting or clearing $table somewhere before it reaches $img, if it's even reaching $img at all. If $table was set, and you're code executed exactly as you said it does, then you wouldn't have a problem, would you? It's not as though you found a bug in the PHP framework. With that said, you must post your relevant code. Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/#findComment-1076884 Share on other sites More sharing options...
mcfmullen Posted June 24, 2010 Author Share Posted June 24, 2010 Here's all the code I've got! <?php if (isset($_GET['item'])) { include("variables.php"); $sql = "SELECT * FROM (.$table LEFT JOIN animalThemes2 USING (Name)) LEFT JOIN animalMethods2 USING (Name) WHERE .$table.Name = '{$item}'"; $result = mysql_query($sql); if ($result) { if (mysql_num_rows($result) > 0) { $row = mysql_fetch_assoc($result); ?> <?php include("header2.php"); ?> <div class="contentArea"> <?php include("breadcrumb.php"); ?> <div class="post" id="post-<?php the_ID(); ?>"> <div class="postHeader"> <center><h2 class="postTitle"><span><?php echo $itemHeader; ?></span></h2></center> <div class="bottom"><div> <span class="postDate"></span> <?php if($arjunaOptions['postsShowAuthor']): ?> <span class="postAuthor"></span> <?php endif; ?> <span></span></a> </div></div> </div> <div class="postMySQL"> <?php include("dateFunction.php"); ?> <?php include("googlecode.php"); ?> <table width="99%"> <tr> <th colspan="4"><h2>Stats</h2></th> </tr> <tr> <td width="21%" rowspan="5"><img src="<?php echo "$img{$row['Photo']}"; ?>"></td> All the includes have nothing to do with the variables, except for variables.php: <?php $table = $_GET['type']; $item = $_GET['item']; $img = 'http://www.cool.com/stuff/' . $table . '/'; $url1='http://www.cool.com/TEST/itemspec2.php?item='; $url2='&type='; $itemHeader = '<a href="' . $url1 . $item . $url2 . $table . '" title="Permalink to ' . $item . '">' . $item . '</a>'; As stated, everything loads fine, except the last line of code where $img is called. As you can see, $table is not being reset anywhere. Where <td width="21%" rowspan="5"><img src="<?php echo "$img{$row['Photo']}"; ?>"></td> the output should be http://www.cool.com/stuff/tableName/photo.png but what I get is http://www.cool.com/stuff//photo.png Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/#findComment-1076903 Share on other sites More sharing options...
jcbones Posted June 24, 2010 Share Posted June 24, 2010 If that is all the code you have, then you should not get anything but parse errors. You have 3 un-closed brackets, one above the SQL statement, and two right below it. Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/#findComment-1076923 Share on other sites More sharing options...
mcfmullen Posted June 24, 2010 Author Share Posted June 24, 2010 I fixed the problem: $img = 'http://www.cool.com/stuff/'.$table.'/'; why that works and the below doesn't is beyond me! Just seems like suddenly it's okay. $img = 'http://www.cool.com/stuff/' . $table . '/'; Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/#findComment-1076924 Share on other sites More sharing options...
jcbones Posted June 24, 2010 Share Posted June 24, 2010 Doesn't make any sense to me either, since PHP ignores whitespace. Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/#findComment-1076927 Share on other sites More sharing options...
mrMarcus Posted June 25, 2010 Share Posted June 25, 2010 I fixed the problem: $img = 'http://www.cool.com/stuff/'.$table.'/'; why that works and the below doesn't is beyond me! Just seems like suddenly it's okay. $img = 'http://www.cool.com/stuff/' . $table . '/'; If that were the case, the rest of your code wouldn't work as your concatenation is the same throughout the script. I know you think that was the issue, but there is something else that was happening here that was causing $table to not be set within $img. Look at another variable you have: $itemHeader = '<a href="' . $url1 . $item . $url2 . $table . '" title="Permalink to ' . $item . '">' . $item . '</a>'; According to your "fix", that variable should not set either due to the spacing in your concatenation. Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/#findComment-1076986 Share on other sites More sharing options...
KevinM1 Posted June 25, 2010 Share Posted June 25, 2010 Again, have you attempted to echo the non-'fixed' $img? Have you turned on error reporting? Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/#findComment-1077000 Share on other sites More sharing options...
mcfmullen Posted June 25, 2010 Author Share Posted June 25, 2010 Look at another variable you have: $itemHeader = '<a href="' . $url1 . $item . $url2 . $table . '" title="Permalink to ' . $item . '">' . $item . '</a>'; According to your "fix", that variable should not set either due to the spacing in your concatenation. It shouldn't have, but it did. That's why i wasn't understanding why it wasn't working. I haven't changed any of my code and the other variables do not interfere. I still don't know why, but the code works now. Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/#findComment-1077008 Share on other sites More sharing options...
KevinM1 Posted June 25, 2010 Share Posted June 25, 2010 It shouldn't have, but it did. That's why i wasn't understanding why it wasn't working. I haven't changed any of my code and the other variables do not interfere. I still don't know why, but the code works now. Again, have you attempted to echo the non-'fixed' $img? Have you turned on error reporting? Your code still has an error in it. Getting expected, 'correct' results does not mean that your code is actually written correctly. As others have said, PHP ignores white space. There's a good chance that, despite expected output being seen, this error/bug will pop up again under a different set of circumstances. You're better off debugging it properly than merely believing your impossible and meaningless fix worked and moving on. Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/#findComment-1077097 Share on other sites More sharing options...
mcfmullen Posted June 25, 2010 Author Share Posted June 25, 2010 Error reporting is turned on. There were no errors reporting at the time. I cannot seem to replicate the same errors with the non-fixed code; in other words, the non-fixed code also seems to work now. Unless the cache wasn't being refreshed in my browser while I was testing the code (which in itself is odd), I can't find an explanation for my results. Believe me, if I could find the bug I would eliminate it but all signs are pointing to this not being a bug at all. Quote Link to comment https://forums.phpfreaks.com/topic/205783-variable-within-variable/#findComment-1077124 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.