Link to home
Start Free TrialLog in
Avatar of mattibutt
mattibuttFlag for United States of America

asked on

poll results with different images

hi buddy

i have a poll which display results using one image i want to display different images for each option

function print_results($poll_id)
{
	
	
	global $tapps_dir;
	global $tapps_pollbar_image_max_width;
 
	if (isset($tapps_pollbar_image_max_width))
		$barmaxwidth=$tapps_pollbar_image_max_width;
	else
		$barmaxwidth=128;
 
	$result = mysql_query("SELECT * FROM tapps_polls WHERE poll_id=$poll_id");
	$poll = mysql_fetch_object($result);
 
	if ($poll)
	{
		echo "<b>$poll->title</b><br>\n";
		echo "<br>\n";
		echo "$poll->description<br>\n";
		echo "<br>\n";
 
		$result = mysql_query("SELECT SUM(counter) AS sum_counter FROM tapps_votes WHERE poll_id=$poll_id");
		$o = mysql_fetch_object($result);
		$sum_counter = $o->sum_counter;
 
		$result = mysql_query("SELECT MAX(counter) AS max_counter FROM tapps_votes WHERE poll_id=$poll_id");
		$o = mysql_fetch_object($result);
		$max_counter = $o->max_counter;
 
		$result = mysql_query("SELECT option_text,counter FROM tapps_votes WHERE poll_id=$poll_id ORDER BY option_id ");
 
		echo "<table cellpadding=0 cellspacing=4>\n";
 
		while ($row = mysql_fetch_object($result))
		{
			echo "<tr>\n";
			echo "<td>$row->option_text</td>\n";
			if ($row->counter > 0 || $row < 2)
			{
				$p = (float)(100*$row->counter/$sum_counter);
				$w = (int)($barmaxwidth*$row->counter/$max_counter);
			}
			else
			{
				$p = 0;
				$w = 1;
			}
			
 
			$s = strip_tags($row->option_text);
				//echo	  "<td> <img src=pic.php?one=".$tapps_dir."<br>";
			
			//printf("<td>%.1f %% </td>\n", $p, $row->counter);
 
		echo "<td><img src=\"".$tapps_dir."poll_bar.jpg\" height=15 width=$w alt=\"$s\"></td>\n";
		printf("<td>%.1f %% </td>\n", $p, $row->counter);
 
 
 
 
			echo "</tr>\n";
 
			
		}
 
 
		echo "</table>\n";
		echo "<br>\n";
		//echo "$sum_counter total votes.<br>\n";
	} else
	{
		echo "Poll does not exist.<br>\n";
	}
	
	
}

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

What do you mean by "option" ?  Are you getting a different alt text from the echo'ed image tag on line 56?
Avatar of NerdsOfTech
Hi Ray, the Asker has a poll that he runs which asks users to vote for one candidate in a set multiple candidates.

1|2|3|4

He wants to show what ratio (or percentage) of votes the candidates received using a bar graph (AKA using an image width to represent the percentage)

Hope that helps

=NerdsOfTech
matt what seems to be the error?
Since you had 4 options on your last Question I changed this line:

                        if ($row->counter > 0 || $row < 3)
function print_results($poll_id)
{
        
        
        global $tapps_dir;
        global $tapps_pollbar_image_max_width;
 
        if (isset($tapps_pollbar_image_max_width))
                $barmaxwidth=$tapps_pollbar_image_max_width;
        else
                $barmaxwidth=128;
 
        $result = mysql_query("SELECT * FROM tapps_polls WHERE poll_id=$poll_id");
        $poll = mysql_fetch_object($result);
 
        if ($poll)
        {
                echo "<b>$poll->title</b><br>\n";
                echo "<br>\n";
                echo "$poll->description<br>\n";
                echo "<br>\n";
 
                $result = mysql_query("SELECT SUM(counter) AS sum_counter FROM tapps_votes WHERE poll_id=$poll_id");
                $o = mysql_fetch_object($result);
                $sum_counter = $o->sum_counter;
 
                $result = mysql_query("SELECT MAX(counter) AS max_counter FROM tapps_votes WHERE poll_id=$poll_id");
                $o = mysql_fetch_object($result);
                $max_counter = $o->max_counter;
 
                $result = mysql_query("SELECT option_text,counter FROM tapps_votes WHERE poll_id=$poll_id ORDER BY option_id ");
 
                echo "<table cellpadding=0 cellspacing=4>\n";
 
                while ($row = mysql_fetch_object($result))
                {
                        echo "<tr>\n";
                        echo "<td>$row->option_text</td>\n";
                        if ($row->counter > 0 || $row < 3)
                        {
                                $p = (float)(100*$row->counter/$sum_counter);
                                $w = (int)($barmaxwidth*$row->counter/$max_counter);
                        }
                        else
                        {
                                $p = 0;
                                $w = 1;
                        }
                        
 
                        $s = strip_tags($row->option_text);
                        //echo    "<td> <img src=pic.php?one=".$tapps_dir."<br>";
                        //printf("<td>%.1f %% </td>\n", $p, $row->counter);
 
                       echo "<td><img src=\"".$tapps_dir."poll_bar.jpg\" height=\"15\" width=\"$w\" alt=\"$s\"></td>\n";
                       printf("<td>%.1f %% </td>\n", $p, $row->counter);
 
                       echo "</tr>\n";                        
                }
 
 
                echo "</table>\n";
                echo "<br>\n";
                //echo "$sum_counter total votes.<br>\n";
        } else
        {
                echo "Poll does not exist.<br>\n";
        }
        
        
}

Open in new window

Oh I see what you mean....
JUST REPLACE THE pollimage[x] value to your desired image to use
option 1 is pollimage[0], etc


function print_results($poll_id)
{
        
$pollimage[0] = 'poll_bar.jpg';
$pollimage[1] = 'poll_bar.jpg';
$pollimage[2] = 'poll_bar.jpg';
$pollimage[3] = 'poll_bar.jpg';
        
        global $tapps_dir;
        global $tapps_pollbar_image_max_width;
 
        if (isset($tapps_pollbar_image_max_width))
                $barmaxwidth=$tapps_pollbar_image_max_width;
        else
                $barmaxwidth=128;
 
        $result = mysql_query("SELECT * FROM tapps_polls WHERE poll_id=$poll_id");
        $poll = mysql_fetch_object($result);
 
        if ($poll)
        {
                echo "<b>$poll->title</b><br>\n";
                echo "<br>\n";
                echo "$poll->description<br>\n";
                echo "<br>\n";
 
                $result = mysql_query("SELECT SUM(counter) AS sum_counter FROM tapps_votes WHERE poll_id=$poll_id");
                $o = mysql_fetch_object($result);
                $sum_counter = $o->sum_counter;
 
                $result = mysql_query("SELECT MAX(counter) AS max_counter FROM tapps_votes WHERE poll_id=$poll_id");
                $o = mysql_fetch_object($result);
                $max_counter = $o->max_counter;
 
                $result = mysql_query("SELECT option_text,counter FROM tapps_votes WHERE poll_id=$poll_id ORDER BY option_id ");
 
                echo "<table cellpadding=0 cellspacing=4>\n";
 
                $rowcount=0;
                while ($row = mysql_fetch_object($result))
                {
                        echo "<tr>\n";
                        echo "<td>$row->option_text</td>\n";
                        if ($row->counter > 0 || $rowcount < 3)
                        {
                                $p = (float)(100*$row->counter/$sum_counter);
                                $w = (int)($barmaxwidth*$row->counter/$max_counter);
                        }
                        else
                        {
                                $p = 0;
                                $w = 1;
                        }
                        
 
                        $s = strip_tags($row->option_text);
                        //echo    "<td> <img src=pic.php?one=".$tapps_dir."<br>";
                        //printf("<td>%.1f %% </td>\n", $p, $row->counter);
 
                       echo "<td><img src=\"".$tapps_dir.$pollimage[$rowcount]."\" height=\"15\" width=\"$w\" alt=\"$s\"></td>\n";
                       printf("<td>%.1f %% </td>\n", $p, $row->counter);
 
                       echo "</tr>\n";                        
			$rowcount++;
                }
 
 
                echo "</table>\n";
                echo "<br>\n";
                //echo "$sum_counter total votes.<br>\n";
        } else
        {
                echo "Poll does not exist.<br>\n";
        }
        
        
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of NerdsOfTech
NerdsOfTech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mattibutt

ASKER

hi NerdsOfTech
i am sorry to reply late i am going to go through with your post and then get back to you my apology once again maybe our timezones are different
No problem. Let me know if you have any more questions.

Sincerely,
=NerdsOfTech
it has done the job thanks so much once again i am sorry i couldn't reply to you early
thank you