Displaying of information from database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AdamTorres
Forum Newbie
Posts: 4
Joined: Thu Nov 18, 2010 4:24 am

Displaying of information from database

Post by AdamTorres »

Hi

I had a few questions stored in the database, I wanna call it out to display in my webpage, but no matter how I do, I can only display 1 ..anyone can help me ??


Code: Select all

<html>
<head><basefont face = 'Arial'></head>
<body>

<h2>Administration</h2>

<?php

if ($_GET['qid'] && is_numeric($_GET['qid'])) {
// include configuration file
include('config.php');

// open database connection
mysql_connect("localhost", "root", "");
mysql_select_db("test");


// get the question
$query = "SELECT qtitle FROM questions WHERE qid = '".$_GET['qid']."'";
$result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
$row = mysql_fetch_object($result);
echo '<h3>'.$row->qtitle.'</h3>';

// reset variables
unset($query);
unset($result);
unset($row);

// find out if any votes have been cast
$query = "SELECT qid, SUM(acount) AS total FROM answers GROUP BY qid HAVING qid = ".$_GET['qid'];
$result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
$row = mysql_fetch_object($result);
$total = $row->total;

// if votes have been cast
if ($total > 0) {
// reset variables
unset($query);
unset($result);
unset($row);

// get individual counts
$query = "SELECT atitle, acount FROM answers WHERE qid = '".$_GET['qid']."'";
$result = mysql_query($query) or die("ERROR: $query. ".mysql_error());

// if records present
if (mysql_num_rows($result) > 0) {
// print vote results
echo '<table border=1 cellspacing=0 cellpadding=15>';

// iterate through data
// print absolute and percentage totals
while($row = mysql_fetch_object($result)) {
echo '<tr>';
echo '<td>'.$row->atitle.'</td>';
echo '<td>'.$row->acount.'</td>';
echo '<td>'.round(($row->acount/$total) * 100, 2).'%</td>';
echo '</tr>';
}

// print grand total
echo '<tr>';
echo '<td><u>TOTAL</u></td>';
echo '<td>'.$total.'</td>';
echo '<td>100%</td>';
echo '</tr>';
echo '</table>';
}
}
// if votes have not been cast
else {
echo 'No votes cast yet';
}

// close connection
mysql_close();
}
else {
die('ERROR: Data not correctly submitted');
}

?>

</body>
</html>
Is it something to do with "looping"??

Thanks

Regards,
Adam.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Displaying of information from database

Post by Celauran »

Based on this:

Code: Select all

$query = "SELECT qtitle FROM questions WHERE qid = '".$_GET['qid']."'";
$result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
$row = mysql_fetch_object($result);
it looks like you're only trying to retrieve one question.
AdamTorres
Forum Newbie
Posts: 4
Joined: Thu Nov 18, 2010 4:24 am

Re: Displaying of information from database

Post by AdamTorres »

Celauran wrote:Based on this:

Code: Select all

$query = "SELECT qtitle FROM questions WHERE qid = '".$_GET['qid']."'";
$result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
$row = mysql_fetch_object($result);
it looks like you're only trying to retrieve one question.

Hi, how should I edit it such that I can retrieve more than 1 question.

Regards,
Adam
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Displaying of information from database

Post by Celauran »

Is the user selecting multiple questions from a form? You could specify multiple questions in $_GET (ie. ?q1=something&q2=something_else&q3=etc) then loop through the $_GET array to build your query string.
AdamTorres
Forum Newbie
Posts: 4
Joined: Thu Nov 18, 2010 4:24 am

Re: Displaying of information from database

Post by AdamTorres »

The user will be able to see the questions and the options available from the database through a webpage. It's like a survey form.
Post Reply