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 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? 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() 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 { } ?> 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); 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 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 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)) 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 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. 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>"; } ?> 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<>"; } ?> 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 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 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. 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 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) .'<>'; } ?> ?? 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 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. 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. 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) 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. 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 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<>"; } ?> Link to comment https://forums.phpfreaks.com/topic/254521-display-php-code/#findComment-1305347 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.