Jump to content

[SOLVED] Being creative with scalars...


sdyates2001

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.