JakkkeM Posted November 2, 2012 Share Posted November 2, 2012 Hi Guys, From what I can tell, there is no other similar inquiry around... I'm completing my Higher School Certificate (final year of high school in New South Wales Australia) and I'm creating a web system for a major. Part of it requires me to have fully dynamic pages for my clients purpose, and I'm using database elements - so basically there is a BB Code site editor that translates [element=menu] to <?php elementCreate("menu"); ?> What I'm finding for some reason I can't contemplate, is that it's just blatantly not working! And when I use view source the <?php elementCreate("menu"); ?> is green as if it's a comment? function bbElement($transform){ $a = array( "/\[element=\"(.*?)\"\]/is", ); $b = array( "<?php echo elementCreate($1); ?>", ); $transform = preg_replace($a, $b, $transform); return $transform; } function elementCreate($element){ $element_fetch = mysql_query("SELECT * FROM `elements` WHERE `element_name` = '".$element."'")or die(mysql_error()); while($element_info = mysql_fetch_array($element_fetch, MYSQL_ASSOC)){ $econt = $element_info['content']; } return $econt; } It's quiet likely I've missed something simple - but all help is appreciated! Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/ Share on other sites More sharing options...
Beeeeney Posted November 2, 2012 Share Posted November 2, 2012 (edited) Hi Guys, From what I can tell, there is no other similar inquiry around... I'm completing my Higher School Certificate (final year of high school in New South Wales Australia) and I'm creating a web system for a major. Part of it requires me to have fully dynamic pages for my clients purpose, and I'm using database elements - so basically there is a BB Code site editor that translates [element=menu] to <?php elementCreate("menu"); ?> What I'm finding for some reason I can't contemplate, is that it's just blatantly not working! And when I use view source the <?php elementCreate("menu"); ?> is green as if it's a comment? $b = array( "<?php echo elementCreate($1); ?>", ); That's not a comment, that's a string. Edit: Looks like you're opening PHP tags inside PHP code. Why? Edited November 2, 2012 by Beeeeney Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1389556 Share on other sites More sharing options...
Muddy_Funster Posted November 2, 2012 Share Posted November 2, 2012 it's not returning elementCreate("right") it is returning elementCreate(right) you need to apply the quotes within the string you build, or create the varaible as a quoted variable. also $1 isn't a valid variable name. variables need to start with either a letter or underscore, see here Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1389558 Share on other sites More sharing options...
JakkkeM Posted November 2, 2012 Author Share Posted November 2, 2012 it's not returning elementCreate("right") it is returning elementCreate(right) you need to apply the quotes within the string you build, or create the varaible as a quoted variable. also $1 isn't a valid variable name. variables need to start with either a letter or underscore, see here The quote marks were my bad in the post - they're not in use in the code what so ever. For the my use of $1 it's fine. That's not a comment, that's a string. Edit: Looks like you're opening PHP tags inside PHP code. Why? None-the-less. The PHP there is within HTML Without the PHP tags it just returns "echo elementCreate(right)" on the page. Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1389564 Share on other sites More sharing options...
Beeeeney Posted November 2, 2012 Share Posted November 2, 2012 The quote marks were my bad in the post - they're not in use in the code what so ever. For the my use of $1 it's fine. None-the-less. The PHP there is within HTML Without the PHP tags it just returns "echo elementCreate(right)" on the page. The PHP is within PHP. $b = array( "<?php echo elementCreate($1); ?>", ); You're already using PHP when declaring the $b variable, right? Then inside the variable contents you're trying to open a PHP tag. Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1389568 Share on other sites More sharing options...
Muddy_Funster Posted November 2, 2012 Share Posted November 2, 2012 right, so you are actualy passing in a constant into the function rather than a variable or string literal?. And if your going to ignore the fact that you are doing something wrong, just because it works for you, what's the point? The PHP is within PHP. $b = array( "<?php echo elementCreate($1); ?>", ); You're already using PHP when declaring the $b variable, right? Then inside the variable contents you're trying to open a PHP tag. No, he is building php tags into a string to echo out at runtime Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1389570 Share on other sites More sharing options...
Beeeeney Posted November 2, 2012 Share Posted November 2, 2012 right, so you are actualy passing in a constant into the function rather than a variable or string literal?. And if your going to ignore the fact that you are doing something wrong, just because it works for you, what's the point? No, he is building php tags into a string to echo out at runtime Fair enough. Please not that my PHP knowledge is limited. Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1389572 Share on other sites More sharing options...
JakkkeM Posted November 2, 2012 Author Share Posted November 2, 2012 right, so you are actualy passing in a constant into the function rather than a variable or string literal?. And if your going to ignore the fact that you are doing something wrong, just because it works for you, what's the point? No, he is building php tags into a string to echo out at runtime Not too sure what your first part means there. Haha. I don't have a background in web development, I'm building on some cool trial-and-error things I've done so I have no clue. And I'm only reluctant to change that $1 because from my understanding in that array with pregreplace it has to be $1, $2 ... "/\[img align=(.*?) src=(.*?)\]/is", ... ... "<img align=\"$1\" src=\"$2\" style=\"margin:10px;\">", ... If there's a better way to do this - I'll change it haha - Just can't off my own back. Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1389573 Share on other sites More sharing options...
Muddy_Funster Posted November 2, 2012 Share Posted November 2, 2012 It's all good, I was just explaining what was happening was all. Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1389574 Share on other sites More sharing options...
Muddy_Funster Posted November 2, 2012 Share Posted November 2, 2012 The first part meens that when you call, for example, runMe(name) then you are saying that name is a declared constant. This is totaly different to runMe("name") as name is a string literal in this case. the forst example php tries to evaluate the constant to a value that should be contained within it at the time of decleration, in the second the string it's self is just passed into the function. Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1389578 Share on other sites More sharing options...
JakkkeM Posted November 2, 2012 Author Share Posted November 2, 2012 I'll try and create a lifeline of the code here... <div class="row-fluid"> <div class="span12">[element="scroll"]</div> </div> I have this on my admin panel which I then addslashes too and it puts it into my database. I then call it back onto my page.php with a query and stripslashes Then I call the function bbElement($the_variable_I_stripped); Ideally the BB element then finds my [element=x] codes and replaces them with <?php elementCreate(x); ?> Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1389579 Share on other sites More sharing options...
JakkkeM Posted November 2, 2012 Author Share Posted November 2, 2012 (edited) The first part meens that when you call, for example, runMe(name) then you are saying that name is a declared constant. This is totaly different to runMe("name") as name is a string literal in this case. the forst example php tries to evaluate the constant to a value that should be contained within it at the time of decleration, in the second the string it's self is just passed into the function. If I'm understanding correctly, then no. It needs to be calling a variable - I have 6 or 7 "elements" that it should changing: menu; logo; left; right; disclaimer. etc... Edited November 2, 2012 by JakkkeM Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1389580 Share on other sites More sharing options...
kicken Posted November 2, 2012 Share Posted November 2, 2012 The way you are doing things right now, you're replacing your bb elements with a string of PHP code. That PHP code never actually executes during the page load so you'll just see it in the page's view source as is. If you want that code to be executed then you need to run the code and replace the bb element with the results of it running, not with a string of PHP code. So you'd run your elementCreate($1) function and replace [element=menu] with what it returns. Something like: function bbElement($transform){ $a = array( "/\[element=\"(.*?)\"\]/ise", ); $b = array( "elementCreate(\"$1\");", ); $transform = preg_replace($a, $b, $transform); return $transform; } Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1389594 Share on other sites More sharing options...
Christian F. Posted November 2, 2012 Share Posted November 2, 2012 (edited) As a side note to Kicken's explanation, it's the "e" modifier to the RegExp that does the difference. You can read more about it in the PHP manual. Edited November 2, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1389601 Share on other sites More sharing options...
JakkkeM Posted November 3, 2012 Author Share Posted November 3, 2012 The way you are doing things right now, you're replacing your bb elements with a string of PHP code. That PHP code never actually executes during the page load so you'll just see it in the page's view source as is. If you want that code to be executed then you need to run the code and replace the bb element with the results of it running, not with a string of PHP code. So you'd run your elementCreate($1) function and replace [element=menu] with what it returns. Something like: function bbElement($transform){ $a = array( "/\[element=\"(.*?)\"\]/ise", ); $b = array( "elementCreate(\"$1\");", ); $transform = preg_replace($a, $b, $transform); return $transform; } If I do this, it just prints element("variable") onto the screen... Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1389896 Share on other sites More sharing options...
kicken Posted November 3, 2012 Share Posted November 3, 2012 http://viper-7.com/ULawLf Working sample. Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1389908 Share on other sites More sharing options...
JakkkeM Posted November 4, 2012 Author Share Posted November 4, 2012 http://viper-7.com/ULawLf Working sample. THANK YOU! Works! Now I run another problem... I have: <span style="font-family:CFSImpact; font-size:31pt; color:#FFF;">Test</span> <span style="font-family:CFSImpact; font-size:31pt; color:#C00;">Title</span> In one of my elements, but it's returning it as plain text? I do have another section that has an image rotator: <div id="myCarousel" class="carousel slide"> <!-- Carousel items --> <div class="carousel-inner"> <div class="active item"> <img src="_images/scroll/Scroll_1.jpg" alt=""> <div class="carousel-caption"> <h4>Third Thumbnail label</h4> <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p> </div> </div> <div class="item"> <img src="_images/scroll/Scroll_2.jpg" alt=""> <div class="carousel-caption"> <h4>Third Thumbnail label</h4> <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p> </div> </div> <div class="item"> <img src="_images/scroll/Scroll_3.jpg" alt=""> <div class="carousel-caption"> <h4>Third Thumbnail label</h4> <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p> </div> </div> </div> <!-- Carousel nav --> </div> That is returning just fine?! What's happening D: Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1390069 Share on other sites More sharing options...
JakkkeM Posted November 4, 2012 Author Share Posted November 4, 2012 Ignore last - forgot to strip slashes! Thanks HEAPS everyone for your help! :happy-04: :happy-04: Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1390070 Share on other sites More sharing options...
JakkkeM Posted November 9, 2012 Author Share Posted November 9, 2012 Kicken Using your working example, how can I now use php in my elements? I.e. if I wanted to put <?php echo $username; ?>, what would I need to do? At the moment I think it's just returning it as a string again... Quote Link to comment https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/#findComment-1391240 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.