ecabrera Posted January 7, 2012 Share Posted January 7, 2012 hello i need help on displaying php code on my website so people can come and just copy it but it wont let me display it Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/ Share on other sites More sharing options...
trq Posted January 7, 2012 Share Posted January 7, 2012 What exactly is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305104 Share on other sites More sharing options...
MasterACE14 Posted January 7, 2012 Share Posted January 7, 2012 I think you're after highlight_string() Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305108 Share on other sites More sharing options...
ecabrera Posted January 7, 2012 Author Share Posted January 7, 2012 ok im using highlight_string() to output a bit of php code but and it does but returns a value of 1 Â <?php if ($_GET['view']){ $id = $_GET['view']; require "scripts/connect.php"; $query = mysql_query("SELECT * FROM php WHERE id='$id'"); $rows = mysql_fetch_assoc($query); $title = $rows['title']; $body = $rows['body']; Â Â Â Â $code = highlight_string($rows['code']); mysql_close(); } ?> <div id="codetitle"> <?php echo $title;?> </div> <p id="body"><?php echo $body;?></p> <?php if($code){ echo "<div id='codeBox'>$code</div>"; }else { } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305109 Share on other sites More sharing options...
MasterACE14 Posted January 7, 2012 Share Posted January 7, 2012 Â Â Â Â $code = highlight_string($rows['code'], true); Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305110 Share on other sites More sharing options...
ecabrera Posted January 7, 2012 Author Share Posted January 7, 2012 ok thanks Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305112 Share on other sites More sharing options...
ecabrera Posted January 7, 2012 Author Share Posted January 7, 2012 ok so i'm saying if there is no code dont dispay that box but if there is display  <?php if(!$code){ }else { echo "<div id='codeBox'>$code</div>"; } ?>  this displays it no matter if there is code or not Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305115 Share on other sites More sharing options...
MasterACE14 Posted January 7, 2012 Share Posted January 7, 2012 if($code == '') Â or... if(empty($code)) Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305116 Share on other sites More sharing options...
ecabrera Posted January 7, 2012 Author Share Posted January 7, 2012 still displaying Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305117 Share on other sites More sharing options...
trq Posted January 7, 2012 Share Posted January 7, 2012 We need to see more code. Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305118 Share on other sites More sharing options...
ecabrera Posted January 7, 2012 Author Share Posted January 7, 2012 <?php if ($_GET['view']){ $id = $_GET['view']; require "scripts/connect.php"; $query = mysql_query("SELECT * FROM php WHERE id='$id'"); $rows = mysql_fetch_assoc($query); $title = $rows['title']; $body = $rows['body']; Â Â Â Â $code = highlight_string($rows['code'], true); mysql_close(); } ?> <div id="codetitle"> <?php echo $title;?> </div> <p id="body"><?php echo $body;?></p> <?php if($code == ''){ }else { echo "<div id='codeBox'>$code</div>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305119 Share on other sites More sharing options...
Andy-H Posted January 7, 2012 Share Posted January 7, 2012 <?php if (isset($_GET['view']) && !empty($_GET['view'])){ // avoid error variable doesn't exist $id = $_GET['view']; require "scripts/connect.php"; $query = mysql_query("SELECT * FROM php WHERE id='$id' AND LENGTH(code)"); Â if ( mysql_num_rows($query) ) Â { Â Â Â $rows = mysql_fetch_assoc($query); Â Â Â $title = $rows['title']; Â Â Â $body = $rows['body']; Â Â Â $code = highlight_string($rows['code'], true); Â } mysql_close(); } ?> <div id="codetitle"> <?php echo $title;?> <> <p id="body"><?php echo $body;?></p> <?php if ( isset($code) ) { echo "<div id='codeBox'>$code<>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305126 Share on other sites More sharing options...
ecabrera Posted January 7, 2012 Author Share Posted January 7, 2012 stilll displays Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305128 Share on other sites More sharing options...
ecabrera Posted January 7, 2012 Author Share Posted January 7, 2012 this was after i add    $code = highlight_string($rows['code'], true); before it use to work Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305129 Share on other sites More sharing options...
Andy-H Posted January 7, 2012 Share Posted January 7, 2012 That's because echo highlight_string('', true); outputs <code><span style="color: #000000"></span></code> Â Did you use the exact code I posted? It should work. Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305130 Share on other sites More sharing options...
ecabrera Posted January 7, 2012 Author Share Posted January 7, 2012 yes but the box still displays Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305131 Share on other sites More sharing options...
Andy-H Posted January 7, 2012 Share Posted January 7, 2012 <?php if (isset($_GET['view']) && !empty($_GET['view'])){ // avoid error variable doesn't exist $id = $_GET['view']; require "scripts/connect.php"; $query = mysql_query("SELECT * FROM php WHERE id='$id' AND LENGTH(code)"); Â if ( mysql_num_rows($query) ) Â { Â Â Â $rows = mysql_fetch_assoc($query); Â Â Â extract($rows, EXTR_SKIP); Â } mysql_close(); } ?> <div id="codetitle"> <?php echo $title;?> <> <p id="body"><?php echo $body;?></p> <?php if ( isset($code) && !empty($code) ) { echo '<div id="codeBox">'. highlight_code($code) .'<>'; } ?> ?? Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305132 Share on other sites More sharing options...
ecabrera Posted January 7, 2012 Author Share Posted January 7, 2012 i tryed and i makes it worse thanks for helping though Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305134 Share on other sites More sharing options...
Andy-H Posted January 7, 2012 Share Posted January 7, 2012 lol how does it make it worse? Note the <>, I think thats something to do with PHPFreaks quote button. Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305136 Share on other sites More sharing options...
PaulRyan Posted January 7, 2012 Share Posted January 7, 2012 This will work I think, untested.  <?PHP  //### If view does not exist, show error message  if(!isSet($_GET['view'])) {   echo 'You have not selected any code to view.';   exit;  }  //### Make $_GET['view'] an integer  $id = intVal($_GET['view']);  if(!$id) {   echo 'Invalid ID to get code.';   exit;  }  //### Include the DB connection  include("scripts/connect.php");  //### Make our query  $query = "SELECT `title`,`body`,`code` FROM `php` WHERE `id` = {$id}";  //### Execute our query  $doQuery = mysql_query($query);  //### If we do find any match, display error, else disaply code  if(!mysql_num_rows($doQuery)) {   echo 'We could not find the code you were looking for.';   exit;  } else {   //### Assign data fetch to $result   $result = mysql_fetch_assoc($doQuery);   //### Echo the code title and body content   echo "<div id='codetitle'> {$result['title']} </div>";   echo "<p id='body'> {$result['body']} </p>";   //### If code exists display it, if it doesn't display a message   if(!empty($result['code'])) {    echo "<div id='codeBox'>{$code}</div>";   } else {    echo 'No code to display';   }  }  ?>  Try it out, tell me how it goes.  Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305147 Share on other sites More sharing options...
ecabrera Posted January 7, 2012 Author Share Posted January 7, 2012 works but does not display the code for the ones that have should i add highlight_code($code) Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305164 Share on other sites More sharing options...
PaulRyan Posted January 7, 2012 Share Posted January 7, 2012 I left this is in to see if you could work it out for yourself, it seems not. You need to try things for yourself, see if you can figure out the issue, anyhow... Â Â Â Â echo "<div id='codeBox'>{$code}</div>"; Change to: Â Â Â echo "<div id='codeBox'> ".highlight_string($result['code'])." </div>"; Â Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305264 Share on other sites More sharing options...
ecabrera Posted January 7, 2012 Author Share Posted January 7, 2012 thanks alot Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305333 Share on other sites More sharing options...
ecabrera Posted January 7, 2012 Author Share Posted January 7, 2012 i have a question why didnt it work just by saying <?php if ($code) { echo "<div id='codeBox'>$code<>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305347 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.