Click to See Complete Forum and Search --> : Images in a PHP poll


Seelmeister
09-01-2005, 09:04 AM
Hi, I've creating a videogames related website, and I'm working on a poll module that comes as part of a CMS. The poll currently allows each IP address to vote once, and then disaplys the results, however the results are just in number format.

I am tring to edit the code so that the results will display an image of varying lengths, depending on how many votes each option has recieved.

I've added this line just after the mySQL query that gets the results from the database;

$total = $db->Execute("SELECT COUNT(*) FROM `sp_polls_options` WHERE `poll_id` = '" . $_REQUEST['id'] . "'");

And this is returning no errors, however, when I try to output the value $total to see if this has worked, all that is displayed in my browser is the word object (see here (http://www.gaming-elite.co.uk/viewpoll.php?id=1) )

Does anyone know what I'm doing wrong?

Also, the poll is displayed on the left menu of my site. When a user votes a message informs them that their vote has been stored, and then redirects back to the index page. How can I change this so that if a user has voted, the results are displayed in place of the poll question?

Thanks in advance.

Genixdeae
09-01-2005, 09:14 AM
you need to give count(*) a name...you can do that by simply changing your statement to the following$total = $db->Execute("SELECT COUNT(*) AS total_cnt FROM `sp_polls_options` WHERE `poll_id` = '" . $_REQUEST['id'] . "'");now in your code you'll call total_cnt

Seelmeister
09-01-2005, 09:24 AM
Thanks for your reply, I've changed my code to reflect yours, and added this to call the count; $total->TOTAL_CNT but nothing is being displayed on the results page.

This is the line where the variable is called;

$poll_options .= "<tr><td bgcolor='#2A2B3A'><font color='#FFFFFF'>".stripslashes($row->TEXT)."</font></td><td bgcolor='#2A2B3A'><font color='#FFFFFF'>$row->COUNT votes </font></td><td bgcolor='#2A2B3A'>$total->TOTAL_CNT</td></tr>";

Seelmeister
09-02-2005, 01:52 AM
Anyone know the correct way to call this into the page?

Genixdeae
09-02-2005, 12:09 PM
all i did was put it in a function, in the function i put it in a while statment//function to grab db info
function db_count($table, $id = "", $id2 = "") {
global $dbconn;
switch($table) {
case "users":
$count_query = "SELECT COUNT(ID) AS total_users FROM `". USERS_TABLE ."`;";
break;
case "newest_member":
$count_query = "SELECT `user_username`, `ID` FROM `". USERS_TABLE ."` ORDER BY `ID` DESC LIMIT 1;";
break;
case "threads":
if(isset($id) && $id != "") {
$count_query = "SELECT COUNT(ID) AS total_threads FROM `". THREAD_TABLE ."` WHERE `thread_forum_id` = '". $id ."';";
}else{
$count_query = "SELECT COUNT(ID) AS total_threads FROM `". THREAD_TABLE ."`;";
}
break;
case "posts":
if(isset($id) && $id != "") {
$count_query = "SELECT COUNT(ID) AS total_posts FROM `". POST_TABLE ."` WHERE `post_forum_id` = '". $id ."';";
}elseif(isset($id2) && $id2 != "") {
$count_query = "SELECT COUNT(ID) AS total_posts FROM `". POST_TABLE ."` WHERE `post_thread_id` = '". $id2 ."';";
}else{
$count_query = "SELECT COUNT(ID) AS total_posts FROM `". POST_TABLE ."`;";
}
break;
case "comments":
switch($id2) {
case "calendar":
$count_query = "SELECT COUNT(ID) AS total_comments FROM `". COMMENTS_TABLE ."` WHERE `comment_source_id` = '". $id ."' AND `comment_type` = 'calendar';";
break;
case "news":
$count_query = "SELECT COUNT(ID) AS total_comments FROM `". COMMENTS_TABLE ."` WHERE `comment_source_id` = '". $id ."' AND `comment_type` = 'news';";
break;
case "photo":
$count_query = "SELECT COUNT(ID) AS total_comments FROM `". COMMENTS_TABLE ."` WHERE `comment_source_id` = '". $id ."' AND `comment_type` = 'photo';";
break;
}
}
if(!($count_result = $dbconn->sql_query($count_query))) {
return false; //If can't run query return nothing
}
$count_numbers = $dbconn->fetch_assoc($count_result);
switch($table) {
case "users":
return $count_numbers["total_users"];
break;
case "newest_member":
return $count_numbers;
break;
case "threads":
return $count_numbers["total_threads"];
break;
case "posts":
return $count_numbers["total_posts"];
break;
case "comments":
return $count_numbers["total_comments"];
break;
}
return false; //If $table does'nt match any cases then return nothing
}//End the functionhope that helsp you figure it out

bathurst_guy
09-02-2005, 09:40 PM
what if you just do
$num = mysql_fetch_array($total);
echo $num;

Seelmeister
09-02-2005, 10:20 PM
If I put this $num = mysql_fetch_array($total);
echo $num; then I get the error messages; Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/gami2674/public_html/sources/polls.class.php on line 19

Any ideas what I'm doing wrong?

theuedimaster
09-02-2005, 10:23 PM
$total = $db->Execute("SELECT COUNT(*) FROM `sp_polls_options` WHERE `poll_id` = '" . $_REQUEST['id'] . "'");

And this is returning no errors, however, when I try to output the value $total to see if this has worked, all that is displayed in my browser is the word object (see here (http://www.gaming-elite.co.uk/viewpoll.php?id=1) )

Does anyone know what I'm doing wrong?


If you output $total, nothing will be displayed because $total is an array. If you want to view the column value COUNT from the row that has a specific poll_id, then to display it you have to echo $total['COUNT']

EDIT**

btw, using db functions, IMO, is a pain in the butt. Its much easier, more fool proof, and more universal if you use plain old php and mysql, and to not use the shortcuts.

Seelmeister
09-02-2005, 10:42 PM
I've tried using your suggestion, and I'm now getting the following message;

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/gami2674/public_html/sources/polls.class.php on line 28

How would I use plain old php and mysql to do this?

bathurst_guy
09-02-2005, 11:41 PM
sorry i missed a part in mine
$num = mysql_fetch_array($total);
echo $num[0];

Seelmeister
09-02-2005, 11:44 PM
Hmm, if I try that I get this error message;

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/gami2674/public_html/sources/polls.class.php on line 19

bathurst_guy
09-03-2005, 12:14 AM
are you sure you are getting any results, if you dont get any results or your sql is incorrect then you receive this error. Do your sql then print_r($total) and what do you get

Seelmeister
09-03-2005, 01:31 AM
Hmm, doing that also returns an error,
Parse error: parse error, unexpected T_VARIABLE in /home/gami2674/public_html/sources/polls.class.php on line 25 ,this time.

Here is the main part of the poll.class.php file, the only parts I have left out are the header and footer. The database in connected to as part of the header.

if (!empty($_REQUEST['id']))

{
$poll = $db->Execute("
SELECT id,title
FROM `sp_polls`
WHERE `id` = '" . $_REQUEST['id'] . "'");

$result = $db->Execute("SELECT * FROM sp_polls_options WHERE poll_id = '$_REQUEST[id]' ORDER BY id");
while ( $row = $result->FetchNextObject() )
{

$total = $db->Execute("SELECT COUNT(*) FROM `sp_polls_options` WHERE `poll_id` = '" . $_REQUEST['id'] . "'");
print_r($total)

$fullwidth = 100;
$poll_percentage =
$image_width = $fullwidth * ($poll_percentage / 100);

$poll_options .= "<tr><td bgcolor='#2A2B3A'><font color='#FFFFFF'>".stripslashes($row->TEXT)."</font></td><td bgcolor='#2A2B3A'><font color='#FFFFFF'>$row->COUNT votes </font></td><td bgcolor='#2A2B3A'></td></tr>";




}

if ($poll->RecordCount() < 1)
{

include "templates/poll_error.inc.php";

} else {

include "templates/poll_view.inc.php";

}

bathurst_guy
09-03-2005, 01:55 AM
first correct your code so far
print_r($total)
should be
print_r($total);
$poll_precentage =
needs to have some value and then a ;
Fix these things then try again and tell me what you have then

Seelmeister
09-03-2005, 02:00 AM
Thanks, that seems to have changed something.

Now this rather large message appears on my page, but everything else seems to be working, the poll results are displayed below the message:

adorecordset_mysql Object ( [dataProvider] => native [fields] => Array ( [0] => 5 [COUNT(*)] => 5 ) [blobSize] => 100 [canSeek] => 1 [sql] => SELECT COUNT(*) FROM `sp_polls_options` WHERE `poll_id` = '1' [EOF] => [emptyTimeStamp] => [emptyDate] => [debug] => [timeCreated] => 0 [bind] => [fetchMode] => 3 [connection] => adodb_mysql Object

.......

create table %s (id int not null) [_genSeq2SQL] => insert into %s values (%s) [_dropSeqSQL] => drop table %s [databaseName] => gami2674_forum ) [_numOfRows] => 1 [_numOfFields] => 1 [_queryID] => Resource id #34 [_currentRow] => 0 [_closed] => [_inited] => 1 [_obj] => [_names] => [_currentPage] => -1 [_atFirstPage] => [_atLastPage] => [_lastPageNo] => -1 [_maxRecordCount] => 0 [datetime] => [databaseType] => mysql [adodbFetchMode] => 0 )

How do I convert this into a single number?

Edit- I deleted a part of the code, as the message was too long to post, hope I haven't left out the important bit

bathurst_guy
09-03-2005, 02:08 AM
Something looks really weird.
I've never used the method of running the sql as you are
$total = $db->Execute("SELECT COUNT(*) FROM `sp_polls_options` WHERE `poll_id` = '" . $_REQUEST['id'] . "'");
where did you get it from, so i can read on it, i use this method
$total = mysql_query("SELECT COUNT(*) FROM `sp_polls_options` WHERE `poll_id` = '" . $_REQUEST['id'] . "'");

Seelmeister
09-03-2005, 02:42 AM
I got this method from iGaming CMS, a gaming geared towards websites about videogames.

I looked at the global.php file, which in included as part of the header, and the tag $db is defined here:

$db = NewADOConnection('mysql');
$db->Connect($db_host,$db_user,$db_pass,$db_name);

When I substitued my current query for your method, and added the print_r($total) bit, this is what was output;

Resource id #30Resource id #31Resource id #32Resource id #33Resource id #34

Edit- If I use your previous suggestions of $num = mysql_fetch_array($total);
echo $num;

Then ArrayArrayArrayArrayArray is output

bathurst_guy
09-03-2005, 04:34 AM
NewADOConnection(); ??
I cant find it in the php manual, is it a function they made in the script aswell?

Seelmeister
09-03-2005, 04:47 AM
I found a file, adodb.inc.php which is included in global php.

After searching I found this on the function;

function &NewADOConnection($db='')
{
$tmp =& ADONewConnection($db);
return $tmp;
}

/**
* Instantiate a new Connection class for a specific database driver.
*
* @param [db] is the database Connection object to create. If undefined,
* use the last database driver that was loaded by ADOLoadCode().
*
* @return the freshly created instance of the Connection class.
*/
function &ADONewConnection($db='')
{
GLOBAL $ADODB_NEWCONNECTION, $ADODB_LASTDB;

if (!defined('ADODB_ASSOC_CASE')) define('ADODB_ASSOC_CASE',2);
$errorfn = (defined('ADODB_ERROR_HANDLER')) ? ADODB_ERROR_HANDLER : false;
$false = false;
if (strpos($db,'://')) {
$origdsn = $db;
$dsna = @parse_url($db);
if (!$dsna) {
// special handling of oracle, which might not have host
$db = str_replace('@/','@adodb-fakehost/',$db);
$dsna = parse_url($db);
if (!$dsna) return $false;
$dsna['host'] = '';
}
$db = @$dsna['scheme'];
if (!$db) return $false;
$dsna['host'] = isset($dsna['host']) ? rawurldecode($dsna['host']) : '';
$dsna['user'] = isset($dsna['user']) ? rawurldecode($dsna['user']) : '';
$dsna['pass'] = isset($dsna['pass']) ? rawurldecode($dsna['pass']) : '';
$dsna['path'] = isset($dsna['path']) ? rawurldecode(substr($dsna['path'],1)) : '';

if (isset($dsna['query'])) {
$opt1 = explode('&',$dsna['query']);
foreach($opt1 as $k => $v) {
$arr = explode('=',$v);
$opt[$arr[0]] = isset($arr[1]) ? rawurldecode($arr[1]) : 1;
}
} else $opt = array();
}


Hope that means something to you as I have no idea ^_^

bathurst_guy
09-03-2005, 04:54 AM
Well i believe none of it is necessary for your site.
Lets go by the KISS method (Keep It Simple Stupid) No offense either :p

Basically do this:

<?php
// Connect to the db
$db = mysql_pconnect('localhost','username','password')
or die ('I cannot connect to the database because: ' . mysql_error());

mysql_select_db ("database_name", $db);


$total = mysql_query("SELECT COUNT(*) FROM `sp_polls_options` WHERE `poll_id` = '" . $_REQUEST['id'] . "'");
$num = mysql_fetch_array($total);
echo $num[0];

?>

Seelmeister
09-03-2005, 05:10 AM
Hmm, I'm not sure, the poll is a very small part of the site and cms, there are many other functions that use the SQL, so I don't want to delete anything I don't understand ^_^

I'll add a new definition into the poll file only, and use your way of connecting to the DB so that if any other functions are dependant on that adodb file (its over 100kb after all, I'm sure the developers wouln't have included it if it wasn't important)

Edit - all thats output is ArrayArrayArrayArrayArray again I'm afraid

Genixdeae
09-03-2005, 10:42 AM
you could use
$num = mysql_num_rows($total);
echo $num;

and in your select statement remove the count() jus have it select *

theuedimaster
09-03-2005, 11:02 AM
Well i believe none of it is necessary for your site.
Lets go by the KISS method (Keep It Simple Stupid) No offense either :p

Basically do this:

<?php
// Connect to the db
$db = mysql_pconnect('localhost','username','password')
or die ('I cannot connect to the database because: ' . mysql_error());

mysql_select_db ("database_name", $db);


$total = mysql_query("SELECT COUNT(*) FROM `sp_polls_options` WHERE `poll_id` = '" . $_REQUEST['id'] . "'");
$num = mysql_fetch_array($total);
echo $num[0];

?>



Change COUNT(*) to COUNT and change echo $num[0] to echo($num['COUNT']);

EDIT*

Actually what is COUNT? Is that an easy way to count the number of rows or is that a name of a column? If it is just a way to count the number of rows, then ignore my suggestion.

bathurst_guy
09-03-2005, 11:04 AM
its a way to count (http://resources.sdstudio.com.au/mysql.php#count) the number of rows

Seelmeister
09-03-2005, 02:28 PM
you could use
$num = mysql_num_rows($total);
echo $num;

and in your select statement remove the count() jus have it select *

I've done this, and I've finally got a number output, but its the number 55,555!

There are 5 votes in total in the poll, so is it just a case of dividing the value of $num but 11,111, or there there yet another problem with my syntax ^_^

bathurst_guy
09-03-2005, 10:20 PM
try as it was, ive had a better look at it now, echo $total[1][0]; or $total[fields][0];

Seelmeister
09-04-2005, 01:34 AM
Ah, its kay, problem sorted, I'd put the echo $num; as part of the loop, so it wans't the number 55,555 being displayed, but the number 5, 5 times :o

Thanks for all your help with this guys.

bathurst_guy
09-04-2005, 02:28 AM
lol np