Jump to content

display php code


ecabrera

Recommended Posts

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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

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