Jump to content

Recommended Posts

I've been working on this one for a while, and can't figure it out at all.

I have a list of variables,
$q1
$q2
...
$q250

These variables all have a value of either 1 or 0.

I need to display these variables in a list with all 1 values saying "Correct" and all 0 values saying "incorrect"

I also need to add up all the 1's in a certain range of these variables (how many correct in the first 25, or from 15-30, etc.

The problem I'm having is setting up a loop that can parse it's way through the list, without me having to type each variable name out.

Here's what I tried:

[code]$i = 1;
while ($q{$i}) {
echo "<TABLE><tr><td>$i</td><TD>";
if ($q{$i} = 0) {
echo "incorrect";
}
else {
echo "correct";
}
echo "</td></tr></table>";
}[/code]

I'm obviously a newbie at this. If I can just get it to call the correct $q variable, I can probably figure everything else out.
Link to comment
https://forums.phpfreaks.com/topic/30925-solved-display-variable-variables/
Share on other sites

It would be easier to use an array...

eg,

[code]$q = array(1 => 0, 2 => 1, 3 => 1); [/code]

ect...

Or using your method,

[code]$num = 250;
for($i = 1; $i <= $num; $i++)
{
if(eval('return \$q' . $i . ';') == 1)
echo "correct";
else
echo "incorrrect";
}[/code]
[quote author=heckenschutze link=topic=118921.msg486394#msg486394 date=1166320747]
It would be easier to use an array...

eg,

[code]$q = array(1 => 0, 2 => 1, 3 => 1); [/code]

ect...

Or using your method,

[code]$num = 250;
for($i = 1; $i <= $num; $i++)
{
if(eval('return \$q' . $i . ';') == 1)
echo "correct";
else
echo "incorrrect";
}[/code]
[/quote]

Thanks so much! if I understand it correctly, would this work?

[code]
$i=1
while (eval('return \q' . $i . ';')) {
        if(eval('return \$q' . $i . ';') == 1){
echo "correct"; }
else {
echo "incorrrect"; }
$i++} [/code]

I want to use a while loop, because the list can vary in size depending on the SQL query. It's anywhere between 25 and 250 long.
Search page:

[code]<form action="srchresults.php" method="post" name="search">
  <p>Referral:
    <input name="referral" type="text" />
    <br />
  First Name:
  <input name="firstname" type="text" />
  <br />
  Last Name:
  <input name="lastname" type="text" />
  <br />
  Phone:
  <input name="phone" type="text" />
  <br />
  E-mail:
  <input name="email" type="text" />
  <br />
  Date:
  <input name="date1" type="text" />
  To:
  <input name="date2" type="text" />
  <br />
  Test Number:
  <input name="testnum" type="text" />
  </p>
  <p>
    <label>submit
    <input type="submit" name="Submit" value="Submit" />
    </label>
    <br />
    </p>
</form>[/code]

[code]
// split POST into a FIELDS array and a SEARCH array
foreach ($_POST as $k => $v) {
$value = $v;
if ($value && $value != "Submit") {
$$k = $k;
$$value = $value;
$fields[] = ${$k};
$search[] = ${$value};
}

}
// Combine feilds and search arrays into a FINALSEARCH array
// this array is created to ease the creation of the SQL Query

reset ($fields);
reset ($search);
foreach ($fields as $kk => $vv) {
$fieldtosearch = $vv;
$searchterm = $search[$kk];
$searchstr = "`$fieldtosearch` LIKE CONVERT( _utf8 '$searchterm' USING latin1) COLLATE latin1_swedish_ci ";
$finalsearch[] = $searchstr;
}

// Create SQL Query

$i = count($finalsearch);
reset ($finalsearch);
foreach ($finalsearch as $k2 => $v2) {
if ($k2 < $i && $k2 != 0) {
$autosql = $autosql." AND ".$v2;
}
else {
$autosql = $autosql." ".$v2;
}
}

$sql = "SELECT * FROM `persuasioniq` WHERE".$autosql;

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","root",""); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("test") or die("Unable to select database"); //select which database we're using

// get results
  $result = mysql_query($sql) or die("Couldn't execute query");
 
 
 
  // begin to show results set
echo "Results";


while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  foreach ($line as $key_value => $col_value) {
      $$key_value = $col_value;
  }}

echo "$testname $firstname $lastname $phone $email $referral $date $score"

?>
<table>
<?php
$i = 1;
echo $q{$i};
echo "q1 is $q1";
echo "q2 is $q2";
while ($q{$i}) {
echo "<tr><td>$i</td><TD>";
if ($q{$i} = 0) {
echo "incorrect";
}
else {
echo "correct";
}
echo "</td></tr>";
}
?>
</table>[/code]

Honestly, I have no idea what I'm doing. I'm kinda making this up as I go, I didn't know ANY php 2 days ago.

I'm trying to create a search page where someone can search through a database, to see their test results. They need to be able to search by whatever field(s) they choose. Then I need to display the results in a nice little form.

The database stores all the data in the search form, along with the correct/incorrect for every question, total score, and the name of the test taken.

There are two tests, one has 20 questions, the other has 221. More tests will probably be added later.

For the 221 question long test, I need to get sub-scores for sections, i.e. questions 1-12 are one section, 13-25 are another, and so on. I need to display all the sub-scores before posting the test results (correct/incorrect).

I probably am over-complicating it, but I don't know any of the functions - most of what I know I learned from hacking together OSCommerce and PHPbb mods.
Perhaps working with an array would be a better option... If that's the case, how can I create an array from the mysql query? I need to pick only the q* fields and put them into a q array. Then I can do a foreach loop.

Or, what if I tried:

[code]
$i=1
while (isset(eval('return \q' . $i . ';'))) {
        if(eval('return \$q' . $i . ';') == 1){
echo "correct"; }
else {
echo "incorrrect"; }
$i++}
[/code]

Would the isset() make it return a positive result, so the loop would go through? I have no idea how the isset() function works.
[quote author=artacus link=topic=118921.msg486534#msg486534 date=1166342681]
[code]
for ($i=1; $i<=count($q); $i++) {
  if($q[$i] == 1) {
    echo "\n$i is correct";
  } else {
    echo "\n$i is incorrect";
  }
}[/code]
[/quote]

artacus, this method is infact [i]1500%[/i] slower than pre-calculating [color=blue]count($q)[/color]... Since the array is  [i]pointlessly[/i] recounted everytime the loop executes. May I suggest calculating it once...

[code]$num = count($q);
for($i = 1; $i <= $num; $i++) ...[/code]
[code]eval('return $q' . $i . ';')[/code]

That seems to be doing what I want it to do. Kindof... I tried this:

[code]while (eval('return $q' . $i) = 1 || eval('return $q' . $i) = 0) {
echo "<tr><td>Question $i</td><td>";
if (eval('return $q' . $i . ';') = 1) {
echo "Correct"; }
else {
echo "Incorrect"; }
echo "</td></tr>";
$i++;
}
[/code]

but it gives an "Unexpected '='" error in the while line. I tried it both with and without the ';' in the eval() but I get the same issue...

Actuallly, my bad. i forgot that a single = means assignment, double == is the checking. I replaced it with this:

[code]while (eval('return $q' . $i . ';') == 0 || eval('return $q' . $i . ';') == 1) {
echo "<tr><td>Question $i</td><td>";
if (eval('return $q' . $i . ';') == 1) {
echo "Correct"; }
else {
echo "Incorrect"; }
echo "</td></tr>";
$i++;
}
[/code]

But now it creates an infinite loop, with everything after the database pull going to incorrect.

It seems now what I need is a way to differentiate between 0 and null. I figure the isset() could do this, but I don't know how to get it to pass through correctly...
Duh, I feel stupid.

!= NULL!!

[code]$i = 1;

while (eval('return $q' . $i . ';') != NULL)
{
echo "<tr><td>Question $i</td><td>";
if (eval('return $q' . $i . ';') == 1) {
echo "Correct"; }
else {
echo "Incorrect"; }
echo "</td></tr>";
$i++;
}[/code]

This worked.

Thanks everyone!
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.