Jump to content

PHP array printing problem

Night_Rider0
Go to solution Solved by Night_Rider0,

Thanks Guys i found the problem the i needed to break the if statments

the code below prints the following pic to the screen but it prints it 4 times have looked for a cause 

no luck 

 

please help

<?php$handle = fopen("vote.txt", "r");if ($handle) {print('<table style="width:75%" border="1">');    while (($line = fgets($handle)) !== false) {#        print_r($array);$array=explode(",",$line); foreach ($array as $n) {print('<tr>' . "\n");print('<td>' . $array[0] . '</td>' . "\n");print('<td>' . $array[1] . $array[2] . '</td>' . "\n");print('<td>' . $array[3] . '</td>' . "\n");print('</tr>');$count++;if($array[3]='Grover'){$a++;}else if($array[3]='Grover'){$a++;}else if($array[3]='Grover'){$a++;}else if($array[3]='Grover'){$a++;}else{print ("Your doing it wrong");}}     }if($count>=$a){print("<br><h1> Grover </h1>"); }else{print ("FAIL");} } else {    print("soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>");} fclose($handle);?>
post-924-0-37789600-1415784775_thumb.jpg

 

 
this is the .csv file it is reading 
 
232323, Night, Rider, Grover123456, fred, long, Grover

 

post-924-0-37789600-1415784775_thumb.jpg

Edited by Night_Rider0
Link to comment
Share on other sites

Link to post
Share on other sites

Hey Night_Rider0

I can't explain the problem with a lot of details but I can show you what I think is the solution.

The line - foreach ($array as $n) - appears to me as if it repeats your code for each array you're using.

You got 4 arrays so you get 4 lines.

 

What are the lines - $count++ - and - $a++ - for? I was just wondering. Might just be me, not being that experienced  :D

 

I tested the code out without that line and it did what you wanted it to do.

So I suggest trying this out;

 

 

 

<?php

$handle = fopen("vote.txt", "r");
if ($handle) {
print('<table style="width:75%" border="1">');
    while (($line = fgets($handle)) !== false) {
#        print_r($array);
$array=explode(",",$line);
 
{
print('<tr>' . "\n");
print('<td>' . $array[0] . '</td>' . "\n");
print('<td>' . $array[1] . $array[2] . '</td>' . "\n");
print('<td>' . $array[3] . '</td>' . "\n");
print('</tr>');
$count++;
if($array[3]='Grover'){
$a++;
}
else if($array[3]='Grover'){
$a++;
}
else if($array[3]='Grover'){
$a++;
}
else if($array[3]='Grover'){
$a++;
}
else
{
print ("Your doing it wrong");
}
}
 
   
}
if($count>=$a){
print("<br><h1> Grover </h1>");
}
else{
print ("FAIL");
}
 
} else {
    print("soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>");
} 
fclose
($handle);
?>

Link to comment
Share on other sites

Link to post
Share on other sites

While unrelated to your issue, is this code intentional?

if($array[3]='Grover') {    $a++;}else if($array[3]='Grover') {    $a++;}else if($array[3]='Grover') {    $a++;}else if($array[3]='Grover') {    $a++;}else {    print ("Your doing it wrong");}

All the if/else if statements are the same and only 1 of them can get called.

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks Guys i found the problem the i needed to break the if statments

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks Guys i found the problem the i needed to break the if statments

 

Do you mean using the 

break;

control structure?

 

 

 

If so, that isn't designed for plain use in a conditional context. Its designed to allow the program to exit from its current loop. In a scenario such as:

do {   //code} while ( //condition is met );

or 

foreach ($x as $key => $val) {     //code }

Also, as pointed out by madknight3, this seems odd:

if($array[3]='Grover') {    $a++;}else if($array[3]='Grover') {    $a++;}else if($array[3]='Grover') {    $a++;}else if($array[3]='Grover') {    $a++;}else {    print ("Your doing it wrong");}
Link to comment
Share on other sites

Link to post
Share on other sites

Also, as pointed out by madknight3, this seems odd:

if($array[3]='Grover') {    $a++;}else if($array[3]='Grover') {    $a++;}else if($array[3]='Grover') {    $a++;}else if($array[3]='Grover') {    $a++;}else {    print ("Your doing it wrong");}

Yeah please explain the logic behind this code

// Gigabyte 990FXA-UD3 // AMD FX-8320 CPU @ 4.3 Ghz (7-21.5 Multiplier) 200.90mhz FSB CPU-Z Validated // Kraken X40 AIO - 2x140mm Push-Pull // 4GB Corsair Vengeance LP - 8GB Avexir Core Series Red 1760Mhz // Sapphire R9 Fury Nitro 1130mhz/4GB 1025mhz (Effective) GPU-Z Validation // Corsair SP2500 2.1 & Microlab Solo 9C Speakers // Corsair K90 Silver - Cherry MX Red & Blue LEDs // EVGA SuperNova 850w G2

Link to comment
Share on other sites

Link to post
Share on other sites

this is an assignment for a class i am taking and this is when i was debugging the code at first before i introduced all the logic to it i was trying to solve the issue of why it would print the data 4 times

after a while i found out by telling the if statement to break after running the code in the brackets it stooped printing the data multiple times not sure if this the proper way to do this but it worked and 

functioned for 99% of what it had to do so i was happy.

 

this code is to take the input form a HTML form that was printed to csv file and count the votes each contestant had since the instructor who made this assignment had a seance of humor or small child 

at the time the vote is between 4 sesame street characters so after it counts each contestants vote it it prints the name of the voter along with who they voted for and determines the winner    

hope this helped someone!

 

Below is the finished code as handed in the only issue it has is determining a 4 way tie

<?php$a=0;$b=0;$c=0;$d=0;$cookie=$_COOKIE["tally"];#$count=0;if($cookie=="ok"){setcookie("tally", "ok", time()+1320);print ("<h1> the votes are tallied </h1>");$handle = fopen("vote.txt", "r");print('<table style="width:75%" border="1">');print('<tr><td>ID #</td><td>Name</td><td>Vote</td></tr>');while (($line = fgets($handle))) {#        print_r($array);$array=explode(",",$line); foreach ($array as $n) {print('<tr>' . "\n");print('<td>' . $array[0] . '</td>' . " \n");print('<td>' . $array[1] . " " .$array[2] . '</td>' . "\n");print('<td>' . $array[3] . '</td>' . "\n");print('</tr>'); $count++;if(chop($array[3]) == 'Grover'){$a=$a+1;break;}else if(chop($array[3]) == 'BigBird'){$b++;break;}else if(chop($array[3]) == 'Elmo'){$c++;break;}else if(chop($array[3]) == 'Oscar'){$d++;break;}else{print ("<td>DThis poll is rigged</td>");}break;} }print ('</table> ');print ('<br><table style="width:75%" border="1"><tr>');print ("<tr><td>Sesame Street character</td><td>Number of votes</td></tr>");print ("<tr><td>Grover</td><td> $a</td></tr>");print ("<tr><td>Big Bird</td><td> $b</td></tr>");print ("<tr><td>Elmo</td><td> $c</td></tr>");print ("<tr><td>Oscar the Grouch</td><td>$d</td></tr>");print ('</table>');$dcount=$count/4;if($a>$b&$a>$c&$a>$d){print("<br><h1> Grover wins </h1>"); }elseif($b>$a&$b>$c&$b>$d){print("<br><h1> BigBird wins </h1>"); }elseif($c>$a&$c>$b&$c>$d){print("<br><h1> Elmo wins </h1>"); }elseif($d>$c&$d>$b&$d>$a){print("<br><h1> Oscar the Grouch wins </h1>"); }# else= if ($dcount==$a & $dcount==$b & $dcount==$c & $dcount==$d){# print("<h1>we have a 4 way tie</h1>");# }elseif($a==$b){print ("<h2>we have a 2 way tie</h2><h3>between Grover & BigBird");} elseif($b==$c){print ("<h2>we have a 2 way tie</h2><h3>between BigBird & Elmo");}elseif($c==$d){print ("<h2>we have a 2 way tie</h2><h3>between Elmo & Oscar the Grouch");}elseif($d==$a){print ("<h2>we have a 2 way tie</h2><h3>between Oscar the Grouch & $a");}elseif($a==$c){print ("<h2>we have a 2 way tie</h2><h3>between Grover & Elmo");}elseif($d==$b){print ("<h2>we have a 2 way tie</h2><h3>between Oscar the Grouch & BigBird");}else {print ("tally error");} #} else {#    print("soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>soiled it <br>");#} fclose($handle);}else{print("<h2>Not Logged in</h2>");print("<br><a href=\"verify.html\">Go to Login<a>");}?> 
Link to comment
Share on other sites

Link to post
Share on other sites

For the 4-way tie you missed a double ampersand which is why it most likely did not work, so it would have looked something like this:
 

if (($dcount == $a) && ($dcount == $b) && ($dcount == $c) && ($dcount == $d)) {    echo "We have a 4-way tie";}

Also instead of doing lots and lots of if statements to try and find the largest value you can simply use:

$mostVotes = max(array_count_values($array[3]));
Link to comment
Share on other sites

Link to post
Share on other sites

You should not have to 'break;' out of a conditional.

 

Take the below as quick example (structure is just to help with viewing),

if (1 == 0) {  // code will NOT run} elseif (1 == 1) {  // code WILL run} elseif (1 == 2) {  // code will NOT run} else {  // code will NOT run}

Here you can see that; if the evaluation of ONE of the conditions is true, it does NOT evaluate the proceeding conditions or run any more of the nested code. Using 'break;' is not necessary and as it is of incorrect syntax, won't actually do anything and may throw PHP a warning error. 

Link to comment
Share on other sites

Link to post
Share on other sites

 

For the 4-way tie you missed a double ampersand which is why it most likely did not work, so it would have looked something like this:

 

if (($dcount == $a) && ($dcount == $b) && ($dcount == $c) && ($dcount == $d)) {    echo "We have a 4-way tie";}

Also instead of doing lots and lots of if statements to try and find the largest value you can simply use:

$mostVotes = max(array_count_values($array[3]));

 

That also,

 

for reference OP:

 

&& = AND

|| = OR

 

(that goes for most languages, I know it does for C)

 

http://php.net/manual/en/language.operators.logical.php

 

PHP has lots of inbuilt functions designed to make your code cleaner and more efficient. The PHP documentation, IMHO, is one of the best. Very clear and easy to follow. (for future reference)  ^_^

Link to comment
Share on other sites

Link to post
Share on other sites

You should not have to 'break;' out of a conditional.

 

Take the below as quick example (structure is just to help with viewing),

if (1 == 0) {  // code will NOT run} elseif (1 == 1) {  // code WILL run} elseif (1 == 2) {  // code will NOT run} else {  // code will NOT run}

Here you can see that; if the evaluation of ONE of the conditions is true, it does NOT evaluate the proceeding conditions or run any more of the nested code. Using 'break;' is not necessary and as it is of incorrect syntax, won't actually do anything and may throw PHP a warning error. 

 

Also to iterate on this about breaks, breaks are only used to escape a loop, so if you had something like:

while ($i =< 4) {    if ($i == 1) {        // do stuff        break;    }    // other code}

The break here will not escape the IF statement but instead will end the entire while loop. You should only use a break within a do, do-while, for, foreach, switch, and while loops, do not use it for anything else because if you are not in a loop while the break is triggered then it will quit the running programme.

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks for the advise guys or gals it is very advise to use in the feature!!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×