GeSHI doesn't work for me

Discussion in 'PHP' started by snowLee, Apr 22, 2009.

  1. #1
    I downloaded geshi in the geshi folder from the root directory and I tried to use the example:

    
    //
    // Include the GeSHi library//
    include_once 'geshi/geshi.php'; 
    //// Define some source to highlight, a language to use
    // and the path to the language files//
     $source = '$foo = 45;
    for ( $i = 1; $i < $foo; $i++ ){
      echo "$foo\n";  --$foo;
    }';
    $language = 'geshi/php.php';
     //
    // Create a GeSHi object//
     $geshi = new GeSHi($source, $language);
     //
    // And echo the result!//
    echo $geshi->parse_code();
    
    PHP:
    but I get only this without color.

    Please, can anyone tell me what I did wrong?
     
    snowLee, Apr 22, 2009 IP
  2. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i see it in color..
     
    szalinski, Apr 23, 2009 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Shows in color here too.
     
    PoPSiCLe, Apr 23, 2009 IP
  4. snowLee

    snowLee Member

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    Yes, with the example from their site, but if I put my code, I get this error .
    What do you think ?

    This is my code, without the html header :

    
    
    <?php
    // Include the GeSHi library//
    include_once 'geshi.php'; 
    //// Define some source to highlight, a language to use
    // and the path to the language files//
     $source = '<?php
    include_once("mysql_connect.php");
    if(isset($_POST['submit'])){
    $problem = FALSE;
    if(empty($_POST['search'])){
    $problem = TRUE;
    echo "<p style=\"color:#FF0000;\">Please enter a search term!</p>";
    }
    
    if(!$problem){
    $text = $_POST['search']; 
    
    $query = "SELECT * from news_posts WHERE title LIKE '%$text%' OR post LIKE '%$text%' OR author LIKE '%$text%'";
    $result = @mysql_query($query);
    
    if($result){
    echo "<h1>found " . mysql_num_rows($result) . " results</h1><br />";
    echo "<ul>";
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $mess=$row['post'];
    $view = 'page.php?id=' . $row['id'];
    echo '<li><a href="' . $view . '">' . $row['title'] . '</a></li>';
    echo '<p style="margin-right:100px;margin-top: 0px;">'.snippet($mess, 200, true).'</p>';
    }
    echo "</ul><br />
    <a href=\"search.php\">Search Again?</a>";
    }
    else
    {
    echo "No posts matched your query";
    }
    }
    }
    else
    {
    ?>
    <form id="search" action="search.php?search" method="post">
    Keyword: <input type="text" name="search" size="30" class="text" /><br />
    <input type="submit" name="submit" value="Search" />
    </form>
    <?php
    }
    ?>';
    
    $language = 'php';
    $path = 'geshi/';
     //
    // Create a GeSHi object//
     $geshi = new GeSHi($source, $language, $path);
     //
    // And echo the result!//
    echo $geshi->parse_code();
    ?>
    
    PHP:
    The error is about this line

    include_once("mysql_connect.php");
     
    snowLee, Apr 24, 2009 IP
  5. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    well you're not backslashing correctly, there's lots of other errors. remember, the source code is becoming a string assigned to a variable ($source = 'string of source code') so you have to make sure it's parsed correctly.
     
    szalinski, Apr 24, 2009 IP
  6. snowLee

    snowLee Member

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    Thank you. So you are saying to put the $source in the addslashes function?
     
    snowLee, Apr 24, 2009 IP
  7. snowLee

    snowLee Member

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    I put manually a backslash before every single quote and worked. Is there a function that does this, I mean that adds backslashes before every single quote?
     
    snowLee, Apr 24, 2009 IP
  8. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    szalinski, Apr 25, 2009 IP
  9. snowLee

    snowLee Member

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #9
    Thank you for help szalinski, but there is another problem now.
    First time I had had just a html file with the code that I wanted to be printed to the screen with geshi. Now I put the content of that file in the database and when I printed to the screen, from database, I got a mess. I don't have colors anymore and the spaces are not anymore where they should be.
    What did the mysql to the file content that the geshi program doesn't work anymore?
     
    snowLee, Apr 25, 2009 IP
  10. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #10
    Try putting the result from the database within <code></code> or <pre></pre> tags before parsing using Geshi?
     
    PoPSiCLe, Apr 25, 2009 IP
  11. snowLee

    snowLee Member

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #11
    This is the result with <pre>. With <code> doesn't happened anything :(.
     
    snowLee, Apr 25, 2009 IP
  12. snowLee

    snowLee Member

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #12
    I tried to put geshi code after I take the code from my data base
     
      <?php
               include_once 'geshi.php'; 
    	    if ((isset($_GET['id'])) && (is_numeric($_GET['id'])) ) {
    	    $id = $_GET['id'];
    	    } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) {
    	    $id = $_POST['id'];
    	    } else {
    	    echo 'Please choose a news post to view.';
    	    exit();
    	    }
    	    $query = "SELECT title, post, author, DATE_FORMAT(date, '%M %d, %Y') as date FROM news_posts WHERE id='$id'";
    	    $result = @mysql_query($query);
    	    if ($result) {
    	    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    	    $mess = stripslashes($row['post']);
    	    $mess = stripslashes($mess);
    	    $mess = preg_replace("/rn/i"," ","$mess");
    		
    	    echo  '<h2>'.$row['title'] . '</h2><p class="blog_text">
    	    Posted by : <b>' . $row['author'] . '</b><br /> on ' . $row['date'] . '<br /><br />'
    	    . $mess . '<br /><hr /></p>';
    		
    $language = 'php';
    $path = 'geshi/';
     //
    // Create a GeSHi object//
     $geshi = new GeSHi($source, $language, $path);
    	    include("comments.php");
    	    }
            ?>	
    
    PHP:
    but still doesn't work. Any idea?
     
    snowLee, Apr 27, 2009 IP