Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

From ASP to PHP

Status
Not open for further replies.

shop73

IS-IT--Management
May 13, 2007
48
0
0
BA
How to translate this ASP code in PHP code?
<%
'Get Poll Results using Filter
Dim arrCount(1000)
For i = 1 to 4
FilterParam = i
rs_poll.Filter = "Choice = " & FilterParam
'Count results
While NOT rs_poll.EOF
arrCount(i) = arrCount(i) + 1
rs_poll.MoveNext()
Wend
Next
%>
 
What type of object is rs_poll? Without knowing what it does, it's difficult to do more than:

Code:
<?php
//Get Poll Results using Filter
$arrCount = array();
for ($i = 1, $i <= 4, $i++)
{
	$FilterParam = i;
	$rs_poll->Filter = "Choice = " . $FilterParam;

	//Count results
	while (! $rs_poll->EOF)
	{
		$arrCount[$i]++;;
		$rs_poll->MoveNext();
	}
}
?>





Want the best answers? Ask the best questions! TANSTAAFL!
 
Parse error: parse error, unexpected ')', expecting ';' on line 12.

line 12

for ($i = 1, $i <= 4, $i++)
 
replace the commas with semicolons.

as rs.poll looks to be an ado db connection you might want to rework that section to use an odbc driver or similar.
 
Oops. Sorry, I didn't try to run the code as I did not have a definition for the class of which rs_poll was an object.

jpadie's right. The line:

[tt]for ($i = 1, $i <= 4, $i++)[/tt]

should read:

[tt]for ($i = 1; $i <= 4; $i++)[/tt]





Want the best answers? Ask the best questions! TANSTAAFL!
 
Fatal error: Call to undefined method stdClass::MoveNext()
 
Line 21: $rs_poll->MoveNext();
 
How to translate this asp code in php?

<%=FormatNumber((arrCount(1)/rs_poll_total*100), 2, -2, -2, -2)%>
 
That would be because we don't have a definition for $rs_poll. As sleipnir asked, what does it do?

Looking up the methods we do know, it looks like (as jpadie pointed out) an ADO Recordset object, so there is probably a set of PHP functions that will handle connecting to the database and retrieving the information. Try looking here to get the specifics, but here is a quick and dirty mock-up.

Code:
$dsn = "hostname";
$user = "username";
$pass = "password";

$conn = odbc_connect($dsn,$user,$pass) or die("Could not connect to database");

$sql = "SELECT Choice FROM [i]table_name[/i]";
$result = odbc_exec($conn,$sql);

$arrCount = array(1 => 0, 2 => 0, 3 => 0, 4 => 0);
while (odbc_fetch_row($result))
{
  $vote = odbc_result($result,"Choice");
  $arrCount[$vote]++;
}

odbc_free_result($result);
odbc_close();

Like I said, I just threw that together, so I'm sure there are errors in it. I've also never worked with ODBC in PHP, so I may have missed an important step or two. In other words, it would be a very good idea to look at the manual for ODBC connections before trying to use that code.
 
Do You know to make poll in PHP like this example above?
 
yes. it is straightforward.

something like this (written for mysql) should be what you are looking for. it needs a lot of bullet proofing and more input validation than I am willing to code.

the table definition is in comment tags at the top of the script.
the connection parameters for your database are in the connectToDatabase() function at the bottom of the script.

Code:
<head>
<script language="javascript">
function addQuestion(){

	var baseElem = document.getElementById("b");
	var p = baseElem.parentNode; 
	var c = p.getElementsByTagName("input");
	if (c[c.length - 1].value != ""){
		var newElem = baseElem.cloneNode(true);
		newElem.Id = '';
		var i = newElem.getElementsByTagName("input");
		i[0].value="";
		p.appendChild(newElem);
	}
}
</script>
<style type="text/css">
body, input, textarea {font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;}
fieldset {width:60%;}
.radio {width: 30px; float: left; text-align:center;}
.label {float:left; text-align:left;}
.question {width: 90%; text-align:center;}
.spacer {visibility:hidden; line-height:1px;}
a, a:visited{text-decoration:none;}
</style>
</head>
<?php
/*
assume a database structure as
create table tablePolls
(pollID int(10) auto_increment primary key,
poll_question longtext);

create table tableResponses
(responseID int(10) auto_increment primary key,
pollID int(10),
responseText varchar(255),
responseVote int(10))
*/
error_reporting (E_ALL);
switchboard();

function switchboard(){
	connectToDatabase();
	$action = isset($_POST['action']) ? $_POST['action'] : (isset($_GET['action']) ? $_GET['action'] : "listPolls");
	
	switch ($action){
	case "displayPoll":
		displayPoll();
	break;
	
	case "listPolls":
		listPolls();
	break;
	
	case "processVote":
		processVote();
	break;
	
	case "processNewResponse":
		processNewResponse();
	break;
	
	case "createNewPoll":
		createNewPoll();
	break;
	case "processNewPoll":
		processNewPoll();
	break;
	}
	
}
function displayPoll(){
	if( isset($_GET['pollID'])){
		$pollID = mysql_real_escape_string(trim($_GET['pollID']));
	} else {
		listPolls();
		exit();
	}
	$sql = "Select poll_question from tablePolls where pollID='$pollID'";
	$sql2 = "Select responseID, responseText from tableResponses where pollID='$pollID'";
	$result = mysql_query($sql) or die(mysql_error());
	$question = mysql_result($result, 0, 0);
	$result2 = mysql_query($sql2) or die(mysql_error());
	while ($row = mysql_fetch_assoc($result2)){
		$responses[] = $row;
	}
	
	echo <<<HTML
<form method="post" action="{$_SERVER['PHP_SELF']}">
<fieldset>
	<legend>Vote Here</legend>
	<div class="row">
		<span class="question">
			$question
		</span>
	</div>

HTML;
	foreach ($responses as $response){
		echo <<<HTML
	<div class="row">
		<span class="radiobox">
			<input type="radio" name="response" value="{$response['responseID']}" />
		</span>
		<span class="label">
			{$response['responseText']}
		</span>
	</div>

HTML;
	}	//end of the foreach
	
	echo <<<HTML

	<div class="row">
		<span class="radiobox">
			&nbsp;
		</span>
		<span class="label">
			<input type="submit" value="submit" name="submit" />
			<input type="hidden" value="{$pollID}" name="pollID" />
			<input type="hidden" value="processVote" name="action"/>
		</span>
	</div>
	<div class="spacer">&nbsp;</div>
</fieldset>
</form>

HTML;
}
function listPolls(){
	$sql = "select pollID, poll_question from tablepolls";
	$result = mysql_query($sql) or die (mysql_error());
	if (mysql_num_rows($result) > 0 ) {
		while ($row = mysql_fetch_assoc($result)){
		echo <<<HTML
<a href="{$_SERVER['PHP_SELF']}?action=displayPoll&pollID={$row['pollID']}">
	{$row['poll_question']}
</a>
<br/>

HTML;
		}
	} else {
		echo "No polls created yet\r\n";
	}
	echo <<<HTML
<br/><a href="{$_SERVER['PHP_SELF']}?action=createNewPoll">Create New Poll</a>

HTML;
}
function processVote(){
	$e = true;
	if (isset($_POST['pollID'])){
		$pollID = mysql_real_escape_string(trim($_POST['pollID']));
	} else {
		$e = false;
	}
	if (isset($_POST['response'])){
		$responseID = mysql_real_escape_string(trim($_POST['response']));
	} else {
		$e=false;
	}
	if (!$e) {
		listPolls();
		exit();
	}
	
	$sql = "Update tableResponses 
			set 
			responseVote = responseVote + 1
			where
			responseID = '$responseID'";
	
	if (!mysql_query($sql)){
		die ("problem updating record");
	}
	
	displayPollResults($pollID);
	exit();
	
}

function displayPollResults($pollID){
	$sql = "Select poll_question from tablePolls where pollID='$pollID'";
	$sql2 = "Select responseText, responseVote from tableResponses where pollID='$pollID'";
	$result = mysql_query($sql) or die(mysql_error());
	$question = mysql_result($result, 0, 0);
	$result = NULL;
	$result = mysql_query($sql2) or die(mysql_error());
	$sum = 0;
	while ($row = mysql_fetch_assoc($result)){
		$sum = $sum + $row['responseVote'];
		$questions[] = $row;
	}
	$result = NULL;
	
	echo <<<HTML
<fieldset>
	<legend>Poll Results - Thank you for voting!</legend>
	<div class="row">
		<span class="question">
			$question
		</span>
	</div>

HTML;
	foreach ($questions as $question){
		$pc = number_format(($question['responseVote']/$sum) * 100,2);
		echo <<<HTML
	<div class="row">
		<span class="radiobox">
			{$question['responseVote']} ({$pc}%)
		</span>
		<span class="label">
			{$question['responseText']}
		</span>
	</div>

HTML;
	}	//end of the foreach
	
	echo <<<HTML

	<div class="spacer">&nbsp;</div>
</fieldset>
HTML;
}
function createNewPoll(){
echo <<<HTML
<form method="post" action="{$_SERVER['PHP_SELF']}">
<fieldset>
	<legend>New Poll Question</legend>
	<div class="row">
		<span class="question">
			<textarea name="question"></textarea>
		</span>
	</div>
	<div class="row">
		<span class="question">
			<input type="submit" value="submit" name="submit" />
			<input type="hidden" value="processNewPoll" name="action"/>
		</span>
	</div>
	<div class="spacer">&nbsp;</div>
</fieldset>
</form>
HTML;
}
function processNewPoll(){
	if (isset($_POST['question'])){
		$question = mysql_real_escape_string(trim($_POST['question']));
	} else {
		createNewPoll();
		exit();
	}
	
	$sql = "Insert into tablePolls set poll_question = '$question', pollID=NULL";
	mysql_query($sql) or die(mysql_error());
	$pollID = mysql_insert_id();
	createNewResponse($pollID);
}
function createNewResponse($pollID){
	$sql = "Select poll_question from tablePolls where pollID='$pollID'";
	$result = mysql_query($sql) or die(mysql_error());
	$question = mysql_result($result, 0, 0);
	echo <<<HTML
<form method="post" action="{$_SERVER['PHP_SELF']}">
<fieldset>
	<legend>Create a new Answer</legend>
	<div class="row">
		<span class="question">
			$question
		</span>
	</div>
	<div id="holder">
	<div class="row" id="b">
		<span class="response">
			<input type="text" name="response[]" />
		</span>
	</div>
	</div>
	<div class="row">
		<span class="response">
			<input type="button" value="Add New Response" onClick="addQuestion(); return false;">
			<input type="submit" value="Save Questions" name="submit">
			<input type="hidden" value="{$pollID}" name="pollID" />
			<input type="hidden" value="processNewResponse" name="action"/>
		</span>
	</div>
	<div class="spacer">&nbsp;</div>
</fieldset>
</form>

HTML;
}

function processNewResponse(){
	$e= true;
	if(isset($_POST['response'])){
		$response = $_POST['response'];
	} else {
		$e = false;
	}
	if(isset($_POST['pollID'])){
		$pollID = mysql_real_escape_string(trim($_POST['pollID']));
	} else {
		$e = false;
	}
	if (!$e){
		die ("error processing responses");
	}
	foreach ($response as $r){
		$values[] = "('".mysql_real_escape_string(trim($r))."',0, $pollID, null)";
	}
	$sql = "Insert into tableResponses (responseText, responseVote, pollID, responseID) values " . implode(",", $values);
	mysql_query($sql);
	listPolls();
}
function connectToDatabase(){
	mysql_connect("localhost", "root", "root") or die (mysql_error());
	mysql_select_db("polls") or die(mysql_error());
}
?>
 
This script works but one error (radio button is on the top of question) like:

O
Red O
Blue O
White

How to put radio button in the row where is question "White"?



 
that's just a css glitch.

change the styles to the following

Code:
body, input, textarea {font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;}
fieldset {width:60%;}
.radiobox {width: 30px; float: left; text-align:center;}
.label {float:left; text-align:left; width: 100px;}
.question {width: 90%; text-align:center;}
.spacer {visibility:hidden; line-height:1px; clear:both;}
.row {clear:both;}
a, a:visited{text-decoration:none;}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top