Dynamic Table Shizzle

Dynamic Table Shizzle

molkman
Owner of George Washington's Prototype Mittens

Marine Warfare Corporal
Find the Hole Participation Medal
Find the Hole II Participation Medal
Tasty Br�twurst Medal
2005 May 2 • 2066
404 ₧
Well-o, I'd like to make my site more dynamic; and therefore, I want to make the archive-type pages (where all games are listed for example) with PHP and MySQL, so I don't have to go into the html code and update the file each time I add a new thing to the table.

Now, I know that it is made with a MySQL database and such, but I don't know how to set up the database. As in, which fields I need and how to put them into the page in the end correctly. I need a thubmnail for each "content", a description, title and author.

Also, I don't know how much work that is, I hope it is not too much.
LET LOVE REIGN
 
 
2009 Jan 4 at 10:10 PST
SuperJer
Websiteman

2005 Mar 20 • 6153
I'd recommend installing something like phpMyAdmin on your site (in a SECURE folder) which will give you a nice web-interface for using your database.

www.phpmyadmin.net/home_page/index.php

You should use something like .htaccess to restrict the folder you put it in so that only you can use it, and not everyone on the net.

Once you have a database, it is very easy to access in php. For example:

<?php
mysql_connect('localhost','username','password');
mysql_select_db('dbname');
$result = mysql_query('SELECT title,description,url FROM articles');
while( list($title,$description,$url) = mysql_fetch_row($result) )
{
echo "<div class=muhhr><a href=$url>$title</a>$description</div>";
}
?>


EDIT: WHOOPS: Forgot something (in red).
 
 
2009 Jan 5 at 17:52 PST — Ed. 2009 Jan 23 at 10:15 PST
SuperJer
Websiteman

2005 Mar 20 • 6153
SECURITY CONCERNS

When using a database on your website, never put user input directly into your SQL queries. For example:

mysql_query('SELECT title,description,url FROM $userinput');

DON'T DO THAT. A user can then input any SQL code they like and it will run against your database.
 
 
2009 Jan 5 at 17:58 PST
molkman
Owner of George Washington's Prototype Mittens

Marine Warfare Corporal
Find the Hole Participation Medal
Find the Hole II Participation Medal
Tasty Br�twurst Medal
2005 May 2 • 2066
404 ₧
Cool, thanks. phpMyAdmin is actually already installed on my account. And, for the thumbnails, shall I simply store the urls to the images in the database and then put it to the table-code stuff?

For the security stuff, do you mean like, input-ing the stuff through the url? Like "index.php?name=shizzle"?
LET LOVE REIGN
 
 
2009 Jan 6 at 09:03 PST — Ed. 2009 Jan 6 at 09:04 PST
SuperJer
Websiteman

2005 Mar 20 • 6153
molkman said:
And, for the thumbnails, shall I simply store the urls to the images in the database and then put it to the table-code stuff?

That is a good way to do it. You can store the images in the database in a BLOB field but I don't think it's usually worth the trouble.

molkman said:
For the security stuff, do you mean like, input-ing the stuff through the url? Like "index.php?name=shizzle"?

Yes. Or from anywhere really. URL vars, POST vars, browser string, etc. should never go directly into a SQL query. All that stuff needs to be sanitized first because a sneaky enough user can put something sneaky in there.

In my code example above you should make sure $userinput is actually a valid table first.
 
 
2009 Jan 6 at 16:56 PST — Ed. 2009 Jan 6 at 16:57 PST
molkman
Owner of George Washington's Prototype Mittens

Marine Warfare Corporal
Find the Hole Participation Medal
Find the Hole II Participation Medal
Tasty Br�twurst Medal
2005 May 2 • 2066
404 ₧
Hot Shizzle Bambizzle, everything works just fine now, thanks alots.

Do you also know how I can arrange the stuff from a database in a grid, instead of a list? You know like, that always a certain amount of "objects" are listed in a row, then a new row is made etcetcetc I hope that is not too hard to do.
LET LOVE REIGN
 
 
2009 Jan 22 at 08:07 PST
SuperJer
Websiteman

2005 Mar 20 • 6153
Something like this will get you six items per row in a table:

$i = 0;
$qresult = mysql_query("SELECT blah blah");
while( $r = mysql_fetch_row($qresult) )
{
  if( $i%6==0 ) echo "<tr>";
  echo "<td>{$r[0]} {$r[1]}</td>";
  if( $i%6==5 ) echo "</tr>";
  $i++;
}


I have got to get better code formatting options on here.
 
 
2009 Jan 22 at 22:34 PST — Ed. 2009 Jan 22 at 22:38 PST
molkman
Owner of George Washington's Prototype Mittens

Marine Warfare Corporal
Find the Hole Participation Medal
Find the Hole II Participation Medal
Tasty Br�twurst Medal
2005 May 2 • 2066
404 ₧
Hot ants! I'll try that out soon. I had to alter your first script, by the way, it turned out to be an infinite loop.
LET LOVE REIGN
 
 
2009 Jan 23 at 04:24 PST
SuperJer
Websiteman

2005 Mar 20 • 6153
It isn't an infinite loop.

mysql_fetch_row() returns FALSE when there are no more rows.
 
 
2009 Jan 23 at 09:58 PST
molkman
Owner of George Washington's Prototype Mittens

Marine Warfare Corporal
Find the Hole Participation Medal
Find the Hole II Participation Medal
Tasty Br�twurst Medal
2005 May 2 • 2066
404 ₧
This one isn't, for sure. I meant your first one, at the beginning of this truck.
LET LOVE REIGN
 
 
2009 Jan 23 at 10:07 PST
molkman
Owner of George Washington's Prototype Mittens

Marine Warfare Corporal
Find the Hole Participation Medal
Find the Hole II Participation Medal
Tasty Br�twurst Medal
2005 May 2 • 2066
404 ₧
Hm, it's not workey for me right now, I get this lucky error:

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/u10877/public_html/awry/head2.php on line 16

Aaand, where do I have to put the code for the visible stuff? Between <td>{$r[0]} and {$r[1]}</td> ?
LET LOVE REIGN
 
 
2009 Jan 23 at 10:16 PST
SuperJer
Websiteman

2005 Mar 20 • 6153
Oh THAT infinite loop.

Yeah, I left out the all-important mysql_fetch_row().

TIME FOR MY SPANKING!

 
 
2009 Jan 23 at 10:17 PST
SuperJer
Websiteman

2005 Mar 20 • 6153
molkman said:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/u10877/public_html/awry/head2.php on line 16

You have bad SQL or no connection to the DB. Put die(mysql_error()); after your mysql_query to see what's wrong.

molkman said:
Aaand, where do I have to put the code for the visible stuff? Between <td>{$r[0]} and {$r[1]}</td> ?

$r is an array of the things in the returned row. In the example I'm just putting elements 0 and 1 from $r in the <td></td>.
 
 
2009 Jan 23 at 10:26 PST — Ed. 2009 Jan 23 at 10:26 PST
molkman
Owner of George Washington's Prototype Mittens

Marine Warfare Corporal
Find the Hole Participation Medal
Find the Hole II Participation Medal
Tasty Br�twurst Medal
2005 May 2 • 2066
404 ₧
So, I have to create an array thing like in the code of your first post in this truck and then use it in the grid code?

Lol, I surely suck at mysql.
LET LOVE REIGN
 
 
2009 Jan 23 at 10:59 PST
SuperJer
Websiteman

2005 Mar 20 • 6153
No. MySQL gives you an array for each row... maybe this will help:

<?php
// first run a query: $qr mysql_query("SELECT name,img FROM things"); // now $qr is a query-result // you can retrieve rows from it one-at-a-time like this: $row mysql_fetch_row($qr); // now $row is an array with two items // $row[0] is the name // $row[1] is the img // which matches the SQL query above // now do something with the row data: list($name,$img) = $row; echo "$name <img src='$img'>";
?>


list() is a handy shortcut for assigning names to each thing in an array.
 
 
2009 Jan 23 at 12:44 PST — Ed. 2009 Jan 23 at 13:05 PST
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
 
 
2009 Jan 23 at 13:04 PST
SuperJer
Websiteman

2005 Mar 20 • 6153
Well it's a bit kloodgey but it works for now...
 
 
2009 Jan 23 at 13:07 PST — Ed. 2009 Jan 23 at 13:08 PST
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
GET "libhdr" LET start() = VALOF { FOR i = 1 TO 5 DO writef("fact(%n) = %i4*n", i, fact(i)) RESULTIS 0 } AND fact(n) = n=0 -> 1, n*fact(n-1)

Does this work? Just checkin'.

No :(
 
 
2009 Jan 24 at 10:31 PST — Ed. 2009 Jan 24 at 10:31 PST
DaveDays
Miley Cyrus Stalker

2008 Jul 21 • 203
153 ₧
8==>

 
 
2009 Feb 24 at 09:12 PST — Ed. 2009 Feb 24 at 09:12 PST
Page [1]