Code for posting each row of database to separate div tags

Hi Guys,

I need some help writing a code that will take each row from my msql database and output it to a separate div tag. There are three columns in the table and ideally I’d like each column to have its own div tag with the rows div tag container.

I hope that makes sense

Many Thanks,

Sam

If I understand what you mean, then this should work nicely:

[php]// Assuming you’ve already connected to your database:
$result = mysql_query(“SELECT * FROM table_name ORDER BY row_primary ASC”);
// Replace “table_name” with the name of your table.
// And replace “row_primary” with the name of the column you marked as the primary key.

while ($row = mysql_fetch_array($result)) {
echo “

”;
echo “
{$row[‘column_one’]}
”; // Replace “column_one” with the name of your first column.
echo “
{$row[‘column_two’]}
”; // Replace “column_two” with the name of your second column.
echo “
{$row[‘column_three’]}
”; // Replace “column_three” with the name of your third column.
echo “
”;
}
[/php]

If you understand the code, then I’d highly suggest removing my comments, they make it hard to read :wink:

Thank You!

I understand it perfectly. I’ve just started a new job and they would like me to learn sql which i have done and absolutely loved it. Made sense to learn a little php as well - I’m learning fast but four days isn’t enough to crack everything!

I have another question about a captcha I’ve built form a tutorial. Its in 3 parts - an html form, captcha.php and a validation form. When i go directly to the captcha.php form it works perfectly. When i go to the html form i cannot get the image to display on my page and can’t for the life of me figure out why. below is the code.

HTML FORM:

[code]

[/code]

captcha.php
[php]<?php

//Start the session so we can store what the code actually is.
session_start();

//Now lets use md5 to generate a totally random string
$md5 = md5(microtime() * mktime());

/*
We dont need a 32 character long string so we trim it down to 5
*/
$string = substr($md5,0,5);

/*
Now for the GD stuff, for ease of use lets create
the image from a background image.
*/

$captcha = imagecreatefrompng("./captcha.png");

/*
Lets set the colours, the colour $line is used to generate lines.
Using a blue misty colours. The colour codes are in RGB
*/

$black = imagecolorallocate($captcha, 0, 0, 0);
$line = imagecolorallocate($captcha,233,239,239);

/*
Now to make it a little bit harder for any bots to break,
assuming they can break it so far. Lets add some lines
in (static lines) to attempt to make the bots life a little harder
*/
imageline($captcha,0,0,39,29,$line);
imageline($captcha,40,0,64,29,$line);

/*
Now for the all important writing of the randomly generated string to the image.
*/
imagestring($captcha, 5, 20, 10, $string, $black);

/*
Encrypt and store the key inside of a session
*/

$_SESSION[‘key’] = md5($string);

/*
Output the image
*/
header(“Content-type: image/png”);
imagepng($captcha);
?>[/php]

Any suggestions?

Thanks

Sam

Hi there,

If that code was copied directly from your files, then your problem probably lies here:

<img xsrc="

(Look at the attribute that you’re using for the image source)

Well you’re probably gonna want to get rid of the ‘x’ in:

<img xsrc="captcha.php" border="0">

And put an image about 70px by 35px and a lightish gray color in the same directory as the captcha.php.
Make sure to call it “captcha.png”, or it wont work.

Thanks Guys,

This fixed the problem. I did think it was slightly strange but having only a small amount of knowledge of HTML and an even smaller amount of writing PHP I asumed the tutorial would be correct. The only issue I have now is that the captcha is not agreeing with the stored session when I enter the characters. Any suggestions? This is my validation code - again taken from the same tutorial

Validation:
[php]<?php
session_start();

//Encrypt the posted code field and then compare with the stored key

if(md5($_POST[‘code’]) != $_SESSION[‘key’])
{
die(“Error: You must enter the code correctly”);
}else{
echo ‘You entered the code correctly’;
}
?>[/php]

Many Thanks,

Sam

Well since in your HTML you’ve got this:

<input type="text" name="captcha_input"/> 

Shouldn’t you change the “$_POST[‘code’]” in:

[php]if(md5($_POST[‘code’]) != $_SESSION[‘key’])[/php]
To “$_POST[‘captcha_input’]”?

Sponsor our Newsletter | Privacy Policy | Terms of Service