Jump to content

Recommended Posts

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.

post-133196-0-62119800-1351853468_thumb.jpg

Link to comment
https://forums.phpfreaks.com/topic/270193-php-and-database-malfunction/
Share on other sites

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 by Beeeeney

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

 

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.

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.

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

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.

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.

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.

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); ?>

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 by JakkkeM

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;
}

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...

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:

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.