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 18:10 UTC
SuperJer
Websiteman

2005 Mar 21 • 6670
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 6 at 01:52 UTC — Ed. 2009 Jan 23 at 18:15 UTC
SuperJer
Websiteman

2005 Mar 21 • 6670
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 6 at 01:58 UTC
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 17:03 UTC — Ed. 2009 Jan 6 at 17:04 UTC
SuperJer
Websiteman

2005 Mar 21 • 6670
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 7 at 00:56 UTC — Ed. 2009 Jan 7 at 00:57 UTC
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 16:07 UTC
SuperJer
Websiteman

2005 Mar 21 • 6670
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 23 at 06:34 UTC — Ed. 2009 Jan 23 at 06:38 UTC
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 12:24 UTC
SuperJer
Websiteman

2005 Mar 21 • 6670
It isn't an infinite loop.

mysql_fetch_row() returns FALSE when there are no more rows.
 
 
 
2009 Jan 23 at 17:58 UTC
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 18:07 UTC
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 18:16 UTC
SuperJer
Websiteman

2005 Mar 21 • 6670
Oh THAT infinite loop.

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

TIME FOR MY SPANKING!

 
 
 
2009 Jan 23 at 18:17 UTC
SuperJer
Websiteman

2005 Mar 21 • 6670
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 18:26 UTC — Ed. 2009 Jan 23 at 18:26 UTC
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 18:59 UTC
SuperJer
Websiteman

2005 Mar 21 • 6670
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 20:44 UTC — Ed. 2009 Jan 23 at 21:05 UTC
Down Rodeo
Cap'n Moth of the Firehouse

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

2005 Mar 21 • 6670
Well it's a bit kloodgey but it works for now...
 
 
 
2009 Jan 23 at 21:07 UTC — Ed. 2009 Jan 23 at 21:08 UTC
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 18:31 UTC — Ed. 2009 Jan 24 at 18:31 UTC
DaveDays
Miley Cyrus Stalker

2008 Jul 22 • 203
153 ₧
8==>

 
 
 
2009 Feb 24 at 17:12 UTC — Ed. 2009 Feb 24 at 17:12 UTC
Page [1]