Tutorial Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Tutorial Script

Post by John Cartwright »

I am having a brain bubble and can't figure out this design.. I have a bunch of code stored in a database and I want to guide the users step by step through tutorails that I will write.

so it will be:

text
code

text
code

etc..

Everythign will be stored in a database, and I'm wondering if anyone has any advice for me..

Also, this will be the function I use for parsing the code..

Code: Select all

<?php
function phpHighlight($code)
	{
	$code = "<?php\n".$code."\n?>";
    $code = stripslashes($code);
    $code = highlight_string($code, true);
    $code = explode('<br />', $code);
   	
    //put a table here and echo the code
}
?>
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

I was thinking you could use 2 tables (one for code, one for plain text), but then I couldn't think of a good (and efficient) way to tell the script which code/text goes with which tutorial, and in what order the chunks go (ie. text1, code1, text2, code2, etc).

You might be better just keeping the code AND text in a blog/text field, and maybe splitting it up by pages before hand.

table tutorials:
tutID | content | page_num

tutorials.php?t=4&p=2

Code: Select all

if(empty($_GET['p'])){ $p = 1; }
else { $p = $_GET['p']; }
$result = mysql_query("SELECT content FROM tutorials WHERE tutID = '" .$_GET['t']. "' AND page_num = '$p' LIMIT 1";
$content = mysql_result($result);
$content = phpHighlight($content);
$page = include("template.inc");
$page = str_replace("{content}",$content,$page);
Hope that helps.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Thats a better answer than I expected.. you've been lots of help TY :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

How can I detect <? and ?> in the code and put tables around the whole code?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Use regex to find php code, then apply a php highlighter fn to the matches. For example this will find anything enclosed in [ php ] ..[ /php ] tags:

"#\[php\](.*?)\[/php\]#si"
Last edited by McGruff on Mon Aug 08, 2005 10:51 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

TY alot mcgruff, I'll try it out first thing tommorow and let ya know the results.

*edit* I have never used the highlighting but do I have to edit php.ini to get the same colouring as this forum.. or is that default ?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

k coo ty
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

K I finally got started on this part of my site... and I'm having some problems with the highlighting color
; Colors for Syntax Highlighting mode. Anything that's acceptable in
<font color="??????"> would work.
highlight.string = #DD0000
highlight.comment = #FF9900
highlight.keyword = #007700
highlight.bg = #FFFFFF
highlight.default = #0000BB
highlight.html = #000000
This is tutorial.php so far

Code: Select all

<?php

$tutorials = new tutorials;

/* NOT USED FOR DEBUGGING

mysql_connect("localhost","root","");
mysql_select_db("main");
$result = @mysql_query("SELECT * FROM tutorials_php") or die('Error performing query: '. mysql_error());	
$row = mysql_fetch_array($result);
$code = $row["code"]; */
	
$code = "<? echo "hello World" ?>";
		$tutorials->phphighlight($code); 
		
?>
This is the function

Code: Select all

<?php
class tutorials {

				function phphighlight($code)
					{
				/*	$code = "<?php\n".$code."\n?>"; */
    				$code = stripslashes($code);
    				$code = highlight_string($code, true);
    				//$code = explode('<br />', $code);
   	
					echo $code;
					}
				}
?>

and it outputs every code I throw at it in highlight.keyword = #007700

Any suggestions on what I'm doing wrong?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

$tutorials = new tutorials();
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Oh ya forgot..but back to the issue at hand
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

doing a search AGAIN on the forums I came across this thread

viewtopic.php?t=22400&highlight=highlight

and came up with this..

Code: Select all

<?php
  $allLines = 'this is a test of the div tagging... [ php]<?php $variable = "check this out ma!"; ?>[ /php] joy joy joy [ php]<?php $variable 
    
    
    = 
    
     "check this out ma!"; ?>[ /php] some more useless text'; 

   $output = preg_replace('|\[ php](.*?)\[/ php]|ise','highlight_string("\\1",true)',$allLines); 

    
   echo $output; ?>
which is exackly I want....

but I sitll can't get it to highlight properly.. :(
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

The below works ok for me:

<?php
$allLines = '
code starts below
[syntax=php]<?php
$variable = \'hello\';
?>[/syntax]
and some more code
[syntax=php]<?php
$foo = \'eek\';
echo $foo;
?>[/syntax]
';
$output = preg_replace('|\[syntax=php](.*?)\[/syntax]|ise',"str_replace('<br />', '', highlight_string('\\1',true))", $allLines);
echo nl2br($output);
?>

(I didn't use bbcode php tags in the post as it mucks up the tags in the example)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

this works on my site/system:

Code: Select all

$message = preg_replace("/\&#1111;php](.*?)\&#1111;\/php]/ise", "'<b>PHP:</b><blockquote style="width: 100%; margin: 3px 8px 3px 16px; color: #000; background-color: #FFF; border: 2px solid #808080; font-size: 8pt;">'.highlight_string('\\1',true).'</blockquote>'", $message);
I'm still fiddling with it though..
Last edited by feyd on Thu Jun 17, 2004 1:31 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Wow thats great.. both of you..

I still have 1 problem tho

PHP syntax highlihging is all coming out 007777 or whatever.. ( read a few posts above ) and
Post Reply