Page 1 of 1

Can someone help me dicipher this code?

Posted: Thu Aug 21, 2003 4:21 pm
by tesseract
Hi;
I am just starting to learn php. I am an ex-CompuSci major, and went far enough to have a decent understaning of programming theory. However, I really haven't written a real program an a while, so my programming understanding has decayed from un-use... :-}
Anyway, I find the fastest way for me to learn a new language is to rip apart other peoples. that said, I am trying to understand the following program from evilwalrus.com.
I tried to visit the website listed in the comment header, but it no longer functions...
anyway, if someone can give me a basic rundown of how this script works, I'd *very* much appreciate it...

I get it that I create a table looking like this:
1 2 3 ... n
title1 title2 title3 ... title n
cnt1 cnt2 cnt3 ... cnt n
hd1 hd2 hd3 ... hd n

but what to I put in the title, content, or header cells?
should I put (x)html code, or links to html code?
is there a character limit in each cell?
if I put in the line:

Code: Select all

echo "$title";
echo "$content";
echo "$heading";
it will output three blocks of html; so, I'd have to divide my site into sections like:

Code: Select all

<html>
<head>
<title> ... </title>
</head>

Code: Select all

<body>
 ...

Code: Select all

...
</body>
</html>
or something similarly cumbersome...?

thanks in advance for your help,
J.

Code: Select all

<?
// Copyright 2003 Liam Getreu

// Make a MySQL Database called "db" with a table named "variables" with
three rows ("id", "title", "heading", and "content"). Insert your data.
Then call the page by going variables.php?id=1 etc.

// ENJOY! Need help? Visit http://www.nr1webresource.com.au/

$id = $_GET['id'];

$db = mysql_connect("localhost", "username", "password");
mysql_select_db("db",$db);

$sql="SELECT * FROM variables WHERE id=" . $id;
$result=mysql_query($sql,$db);

$num = mysql_num_rows($result);
$cur = 1;

while ($num >= $cur) {

$row = mysql_fetch_array($result);

$id = $row["id"];
$title = $row["title"];
$content = $row["content"];
$heading = $row["heading"];

echo "What ever vars you want to be outputted";

$cur++;
}

?>

Posted: Thu Aug 21, 2003 4:31 pm
by oraknabo

Code: Select all

<? 
//THIS EXPECTS AN ID TO BE POSTED THROUGH GET
$id = $_GET['id']; 

//THIS CONNECTS TO THE DATABSE THROUGH A HANDLE
$db = mysql_connect("localhost", "username", "password"); 
mysql_select_db("db",$db); 

//CREATE THE QUERY TO RUN THROUGH MYSQL WITH THE PASSED ID
$sql="SELECT * FROM variables WHERE id=" . $id; 

//RUN QUERY, STORE RESULT IN A VARIABLE
$result=mysql_query($sql,$db); 

//COUNT NUMBER OF ROWS IN RESULT
$num = mysql_num_rows($result); 
$cur = 1; 


//OUTPUT EACH ROW OF THE RESULT (there are better ways)
while ($num >= $cur) { 

//FEED EACH PART OF THE MYSQL RESULT INTO A DIFFERENT VARIABLE
$row = mysql_fetch_array($result); 
$id = $row["id"]; 
$title = $row["title"]; 
$content = $row["content"]; 
$heading = $row["heading"]; 

//PRINT RESULTS
echo "What ever vars you want to be outputted"; 

//INCREMENT row ID
$cur++; 
} 

?>
Instead, I'd do these 2 parts differently:

Code: Select all

<?php
//CREATE THE QUERY TO RUN THROUGH MYSQL WITH THE PASSED ID
//specify each item you want from the database
$sql="SELECT column1, column2, column3 FROM variables WHERE id=" . $id; 

//OUTPUT EACH ROW OF THE RESULT ---to end of script---
while(list($column1, $column2, $column3 )=mysql_fetch_row($result)){
  //PRINT RESULTS
    echo "What ever vars you want to be outputted"; 
}
?>