Forum Moderators: coopster
After entering the dark realms of PHP and mySQL, I have it all setup and uploaded a text file for my db. On top of this, ive managed to actually retrieve results from the db....no small feat for moi!
Anyways, Im looking for good tutorials on how to "design" your PHP. ie when I got the info from the db it is more or less "dumped" on the screen. I would be greatful if anyone knows of a good tutorial/information source that could help me with PHP and the design aspects of it
Thanks
First up, the php has nothing at all to do with 'design'.
Just plain old html will handle that, your script is probably returning a set of variables (containers for information) and printing them to the screen with no formatting.
if one of you fields is say, 'first_name' then try this:
<?
print("<p>".$first_name."</p>");
?>
Some cool places are:
www.php.net // the best ever
www.devshed.com
www.phpbuilder.com
www.onlamp.com
If you still have trouble, post the script and I'll use it to show you in more detail.
Regards
Nick
If you want a quick and dirty method, Madcat asked roughly the same question a while back. I posted an answer which killed the thread (it's currently the last post), so it must have been enough to either answer the question or drive everyone away...
[webmasterworld.com...]
Anyway, it's meant to get someone started. If you want to do it *right*, that's another matter and, in addition to the resources that Nick mentioned, I would suggest some specifics like:
[zend.com...]
Most of the articles at
[zend.com...]
especially
This is the current "state" for what is meant to be a future biz directory
$result = mysql_query("SELECT `name`,`address`,`postcode`,`telephone`,`fax`,`url`,`info` FROM `businesses` WHERE 1 AND `townid` = 1 AND `name` LIKE 'a%' LIMIT 0, 10", $connection);
// Fetch each row of the results into an array $row
while ($row = mysql_fetch_array($result))
{
print("<p>"$name"</p>");
print("<p>"$address"</p>");
print("<p>"$postcode"</p>");
print("<p>"$telephone"</p>");
print("<p>"$fax"</p>");
print("<p>"$url"</p>");
print("<p>"$info"</p>");
}
The query is where "1" is a town and another "1" is a category (they are in another table and are used to minimise repeating data)
part of my primitive design layout is a 3 column layout....2 at 40% and one at 20% for navigation.
The way the PHP currently stands, all results "fall" into one cell and are displayed downwards...according to the law of gravity
Perhaps I want #1 result to be in left cell, #2 in right #3 below left and so on.....
Is this easily done with PHP? :) Also, is the above code "OK"?
<?php
while ($row = mysql_fetch_array($result))
{
echo "<p><table>";
echo "<tr><td>".$row[0]."</td>";
echo "<td>".$row[1]."</td></tr>";
echo "<tr><td>".$row[2]."</td>";
echo "<td>".$row[3]."</td></tr>";
echo "<tr><td>".$row[4]."</td>";
echo "<td>".$row[5]."</td></tr>";
echo "<tr><td colspan=\"2\">".$row[6]."</td></tr>";
echo "</table></p>";
}
?>
This will spit out 1 table of 4 rows for each complete record and then repeat the pattern for the next record.
I modified your code somewhat. I prefer to use echo over print and I use concantenation to connect strings and variables - a good habit (IMO) to get into for it will help you when you get into the hot and heavy of string manipulation.
The biggest change I made is that of the variables. Note that I'm calling for the content based on it's position within the array rather than by name. The reason is you haven't defined the key (the pointer to the data) with a name so the array only knows each position by the key value [0] for first position of the current record = $name, [1] for the second position = $address, etc...
HTIWYLF
GB
yeah this is like my 4th day using PHP/mySQL so its a little fresh and new.
next on the agenda is
1)piecing together a form to query various aspects of the index
2)finding out more about relational db's using mysql
3)creating the "next 10 results" button and such like
all new to me, will get there!
HTIWYLF
Hope This Is What You're Looking For.
It appears to me you've got a pretty good start.
Re: next 10 results - check out [phpbuilder.com...] for a good tutorial on limiting your results display but also building the navigation for "the next 10 results".
Now, basically, I dont see why everything on the site should be called from a database
So im just working out a template for the "true" db results to be situated within. Sorta gonna have to look into CSS a bit deeper to make sure this code is as light as possible......while at the same time digging deeper with PHP/mySQL
Now that Ive got the basics, (this thread is a good beginners guide!!) it looks like im going to have to read up and go along trial and error......on the learning curve
Thanks all for the help
The big disadvantage to storing the pages in the DB is that you have to worry more about having control characters and quotes and so forth in your text and it is more difficult to throw in and execute php statements.
On a "content" site I think it's easier to do a combination. In order to keep everything straight, I build an "admin" page that lets you associate a file with categories etc etc etc. Basically, I put page titles, keywords and description (for meta) in the DB. The basic content goes in an include file. That seems to work pretty well.
On an e-commerce site with lots of products with short descriptions, I would put pretty much everything but images in the DB.
Tom
For example, navigation which remains the same on every page. I know I could have done this with straight SSI but PHP made it so much more elegant AND easier to determine browser type so I could account for the differences in rendering the menus.
All in all, though, I think you're on the right track. A mix of static pages is the right approach in many cases. Just because you CAN make it work with PHP & MySQL doesn't mean you should!;)
Cheers!
i was looking for a way to display mysql query results in a table consisting of three rows and three columns and hit this page...
i read lorax's code and it seemed to me that it would simply output a list of tables, each one containing one result and when i tried it that's exactly what I got...
am I missing something?
thanks in advance
$numcols = 3; // how many columns to display
$numcolsprinted = 0; // no of columns so far
// get the results to be displayed
$query = "SELECT * FROM tablename WHERE condition=value ORDER BY whatever DESC LIMIT";
$mysql_result = mysql_query($query, $mysql_link);
// get each row
while($row = mysql_fetch_row($mysql_result))
{
//get columns
$id = $row[0];
$foo = $row[1];
$bar = $row[2];
$foobar = $row[3];
if ($numcolsprinted == $numcols) {
print "</tr>\n<tr>\n";
$numcolsprinted = 0;
}
// output row from database
print "<td>".$foobar."</td>\n";
// bump up row counter
$numcolsprinted++;
} // end while loop
// push out some cells to even the table out
$colstobalance = $numcols - $numcolsprinted;
for ($i=1; $i<=$colstobalance; $i++) {
print "<TD> </TD>\n";
}