Jump to content

Recommended Posts

My PHP coding has become more advanced since yesterday, but I am still thinking like perl.

 

 

Instead of wasting space, I want to ask if $Questions['ch1...13'] is not equal to "", then to perform a statement block.

 

Could you please assist me with where I am going wrong?

 

$count = "";
while ( $count < 13 ) {
if ( $Questions['ch$count'] != "" ) {

	$PositiveFirstCriteria += 1;
	echo "<p>Count: $count, $PositiveFirstCriteria</p>";
}

$count += 1;
}

 

Thanks in advance to anyone who takes the time to respond!

Link to comment
https://forums.phpfreaks.com/topic/142699-solved-being-creative-with-scalars/
Share on other sites

Something tells me it has to do with your use of ch$count, PHP isn't gonna know what that is.  Perhaps ch.$count will work but I think you'll need to rethink your logic there.

 

Also you can use $PositiveFirstCriteria++ instead of += 1.  Same thing for $count.

Just noticed as well, if you have 13 questions then you will need to change your < (less than) to <= (less than or equal to) otherwise it will skip number 13, although a for loop is better suited for looping through an array like you are.

<?php

$Questions["ch1"] = "ch1";
$Questions["ch2"] = "ch2";
$Questions["ch3"] = "ch3";
$Questions["ch4"] = "ch4";
$Questions["ch5"] = "ch5";
$Questions["ch6"] = "ch6";
$Questions["ch7"] = "ch7";
$Questions["ch8"] = "ch8";
$Questions["ch9"] = "ch9";
$Questions["ch10"] = "ch10";
$Questions["ch11"] = "ch11";
$Questions["chDontCountMe12"] = "ch12";
$Questions["ch13"] = "ch13";

$PositiveFirstCriteria = 0;

for($i = 1; $i <= 13; $i++)
{
    if($Questions["ch".$i])
    {
        $PositiveFirstCriteria++;

        echo "<p>Count: ". $i .", ". $PositiveFirstCriteria ."</p>";
    }
}
?>

 

Outputs

 

Count: 1, 1

Count: 2, 2

Count: 3, 3

Count: 4, 4

Count: 5, 5

Count: 6, 6

Count: 7, 7

Count: 8, 8

Count: 9, 9

Count: 10, 10

Count: 11, 11

Count: 13, 12

<?php

$Questions["ch1"] = "ch1";
$Questions["ch2"] = "ch2";
$Questions["ch3"] = "ch3";
$Questions["ch4"] = "ch4";
$Questions["ch5"] = "ch5";
$Questions["ch6"] = "ch6";
$Questions["ch7"] = "ch7";
$Questions["ch8"] = "ch8";
$Questions["ch9"] = "ch9";
$Questions["ch10"] = "ch10";
$Questions["ch11"] = "ch11";
$Questions["chDontCountMe12"] = "ch12";
$Questions["ch13"] = "ch13";

$PositiveFirstCriteria = 0;

for($i = 1; $i <= 13; $i++)
{
    if(strlen(trim($Questions["ch".$i])) > 0)
    {
        $PositiveFirstCriteria++;

        echo "<p>Count: ". $i .", ". $PositiveFirstCriteria ."</p>";
    }
}
?>

Just changed one thing

 

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.