"PHP" uses Jpgraph to complete the general user portion of the voting system

Source: Internet
Author: User
Tags php write

In this article, a "PHP" Voting system Administrator section, increase voting and delete votes (click to open the link)

The last three links completed only two of the administrator sections


Now complete the last of a normal user this one, also is not logged in the system part, please add yourself


I. BASIC OBJECTIVES

The general user section of the voting system is as follows:


First show the existing voting list to the user, then click into one of the polls to be able to vote

Voting is a single choice, not set to multi-select, of course, you set as a multi-select also line, anyway, each page is a form,

Click on the "Vote" button, ask the user whether it is OK, the vote is successful Oh, otherwise nothing happens

When any one of the polls is larger than 1 o'clock, two charts, a histogram and a pie chart are displayed, indicating the number of votes and the proportion



Second, the basic idea

Histogram and pie chart with Jpgraph

Jpgraph download and install similar to other plugins, drag what you need into the download directory.

First open the Jpgraph's official website (click the Open link) to select Download->download jpgraph


Then choose the first one, I also uploaded a copy for everyone (click to open the link)


Although this thing is not zip or rar to end, but with the latest winrar can open and unzip, unzip the inside of the SRC put to the site directory, and renamed to Jpgraph folder can be detailed in a "PHP" Voting system Administrator section, increase voting and delete votes Directory structure (click to open link)



Third, the production process

1, voteindex.php

This is a page that shows all the voting lists, more simple than deleting the poll list page delvote.php, without the delete button

Pay attention to each hyperlink that comes out of the query, attach the? Get suffix, pass the Get method, give the vote.php the parameter that the user actually opens that poll

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

2, vote.php

Using the request method to get the ID parameter passed by voteindex.php, it's starting to work.

To investigate the voting title and voting description of the poll and the various attributes of each sub-selection, create a form,

and to construct a number of votes for each sub-option, a string similar to opt1=1&&opt2=99&&opttotal=2 is appended to the link that introduces the picture, where Opttotal is the number of options available for this poll. If this vote is only 2 options, then opttoal=2. Here are the work needs for jpgraph. For details, see the code:

<?php$pid=$_request["id"]; $con =mysql_connect ("localhost", "root", "root"), if (! $con) {die ("Connection failed! ");} mysql_select_db ("Test", $con); mysql_query ("Set names UTF8"); $ptitle; $ptext; $result =mysql_query ("SELECT * FROM Voteparent where id= ". $pid."; "); while ($row =mysql_fetch_array ($result)) {$ptitle = $row ["title"]; $ptext = $row ["text"];}? ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

This page also has a certain small problem, if the user does not select any option, click on the vote does not have null value processing,

Specific can be in the foreground, with JavaScript processing, specifically can refer to the "JavaScript" form of instant verification, unsuccessful submission (click to open the link)


3, jpgraph.php

This page is used to draw a histogram, and then be vote.php with the IMG tag reference, the following code:

<?php//This page can not have any HTML and JS elements, otherwise jpgraph will not work//Although only 2 pages are introduced, but all the files in the Jpgraph folder must be preserved, otherwise jpgraph will not work include ("jpgraph/ Jpgraph.php "), Include (" jpgraph/jpgraph_bar.php ");//Create an array $optarr, which is used to store the variable $optarr=array () passed over by the Get method; 1 for//x axis, 2,3,4,5,6 to represent $xaxisarr=array (), $opttotal =$_request["Opttotal"];for ($i =1; $i < $opttotal; $i + +) {$optarr [$i -1]= $_request["Opt${i}"; $xaxisarr [$i -1]= $i;} The maximum value is calculated to determine the maximum value of the histogram $optmaxcount=max ($optarr);//Image size 350x200px$graph=new graph (350,200, "Auto");//Specify Action $graph- >setscale ("Textlin");//The following two lines are to set the style, false and true have different styles $graph->setbox (false); $graph->ygrid->setfill ( FALSE); The//y axis has a value of 4, 0,1/3,2/3,1$graph->yaxis->settickpositions (array (0, $optmaxcount/3,$) of the maximum number of votes for the sub-option. OPTMAXCOUNT*2/3, $optmaxcount));//The next three lines means that the column length of the x-axis is the NO. 0 item of the $optarr array, the 1th item ... $b 1plot = new Barplot ($optarr); $gbplot = new Groupbarplot (Array ($b 1plot)), $graph->add ($gbplot);//The Edge of the column is a white $b1plot->setcolor ("Black");//The column color is #aa0000, #00aa00 ... If there are $b1plot->setfillcolor (' #aa0000 ', ' #00aa00 ', ' #0000aa ', ' #aaaa00 ', ' #aa00aa ', ' #00aaaa', ' #000000 ', ' #cc0000 ', ' #00cc00 ', ' #0000cc '));//The title of Chinese needs to write $graph->title->setfont (ff_simsun,fs_bold) as follows; Graph->title->set (Iconv ("UTF-8", "gb2312", "Ticket Situation"));//Specify Action $graph->xaxis->setticklabels ($XAXISARR); $ Graph->stroke ();? >

4, jppie.php

This is used for appeased, in general, than the jpgraph.php of the histogram is a little simpler,

Part of the code principle is the same as jpgraph.php

<?php//appeased, the introduction of the file is different, but still need the entire Jpgraph folder to support the work include ("jpgraph/jpgraph.php"); Include ("Jpgraph/jpgraph_ Pie.php ");//create an array to store the data sent by the Get Method $optarr=array (); $opttotal =$_request[" Opttotal "];for ($i =1; $i < $opttotal; $i + +) {$ optarr[$i -1]=$_request["Opt${i}"];} $graph = new Piegraph (350,250), $graph->title->setfont (ff_simsun,fs_bold), $graph->title->set (Iconv (" UTF-8 "," gb2312 "," ratio of votes ");//do not use their own calculation ratio, directly into the, will automatically calculate $P1 = new Pieplot ($optarr); $graph->add ($p 1); $p 1-> Setslicecolors (Array (' #aa0000 ', ' #00aa00 ', ' #0000aa ', ' #aaaa00 ', ' #aa00aa ', ' #00aaaa ', ' #000000 ', ' #cc0000 ', ' #00cc00 ' , ' #0000cc '));//The pie chart is placed in the 350x250 canvas in the transverse 50%, portrait 60% position $p1->setcenter (0.5,0.6); $graph->stroke (); >

Through the above two processing steps, in the main page vote.php with an IMG tag to introduce this two PHP, the real image of the page is good


5, votehandel.php

This is the user on the polling page, after selecting an option, click OK to vote on the page

After the hardest jpgraph, it's not hard,

In the operation of the database, the Votechildren table, the corresponding sub-option, with the UPDATE statement to the number of votes plus 1 can be

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">


iv. Outlook

Since then, along with the "PHP" Voting system Administrator section, increase voting and delete voting (click the Open link), the entire voting system core bone is completed

What is needed is to avoid SQL injection and JavaScript with some null processing and the same Xajax technology to verify whether or not to repeat,

Plus login system, with the server object to take away the user's IP to prevent duplicate voting, this technology can refer to my previous "PHP" login system and output browser information (click the Open link), to the reality


"PHP" uses Jpgraph to complete the general user portion of the voting system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.