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
Share on other sites

<?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

Link to comment
Share on other sites

<?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

 

Link to comment
Share on other sites

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.