简体   繁体   中英

If Statement within While Loop

I've made some code which shortens a name if it is over so many characters (shortens first name for initial, removes middles names, and if last name is overall too long shortens until fits).

I had version of this working (Which is posted after my current code) but now that I try it with some while loops it complains about an unexpected { on the first if in the loop (Line 9).

Current Code:

<?php
$name = "billy bobby sharkingflardo herdaderpingtonning";       

        while ((strlen($name))>17)
            {
            $nameEx = explode(" ",$name);

            if ((strlen($nameEx[0])>1)
                {
                $nameEx[0] = substr($nameEx[0], 0, -(strlen($nameEx[0])-1));
                }

            while ((count($nameEx))>2)
                {
                unset($nameEx[1]);
                }

            if ((strlen($nameEx[1]>15))
                {
                substr($nameEx[1], 0, -1);
                }
            $name = implode(" ",$nameEx);       
            }

        echo $Name
?>

I am still fairly new to PHP, but I don't see anything wrong with this, unless you can't place if within while loops in some circumstances, but from doing some research I have not found that to be the case.

My old code which was working is as follows:

<?php
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('amazondb', $conn); 
$result = mysql_query("SELECT * FROM imported_orders");

while($row = mysql_fetch_array ($result))
{
    $nL = strlen($row['RecipientName']);
    echo "<br><b>Name:</b> ".$row['RecipientName']."<br>";
    echo "<b>Names Length:</b> ".$nL."<br>";

    if ($nL>17)
    {
        $nameEx = explode(" ",$row['RecipientName']);
        if ((count($nameEx))>2)
        {
            unset($nameEx[1]);
            $nameEx[0] = substr($nameEx[0], 0, -(strlen($nameEx[0])-1));
        }
        $name = implode(" ",$nameEx);
        echo "<b>New Name:</b> ".$name."<br>";
        echo "<b>New Length:</b> ".strlen($name)."<br>";        
    }

}

?>

This code is to work with my sql database, but it doesn't take into account multiple middle names and if the last name is on its own too long.

I'm sure it must be something to do with the while loop, but have no idea what.

Any help much appreciated, thanks.

Functionality aside, your code is littered with syntax errors and unnecessary parentheses. Try this:

$name = "billy bobby sharkingflardo herdaderpingtonning";

while (strlen($name) > 17) { // removed extra parentheses around strlen()
    $nameEx = explode(' ', $name);

    if (strlen($nameEx[0] > 1)) { // removed extra and fixed unclosed parentheses
        $nameEx[0] = substr($nameEx[0], 0, -(strlen($nameEx[0]) - 1));
    }

    while (count($nameEx) > 2) { // removed extra and fixed unclosed parentheses
        unset($nameEx[1]);
    }

    if (strlen($nameEx[1] > 15)) { // removed extra and fixed unclosed parentheses
        substr($nameEx[1], 0, -1);
    }

    $name = implode(' ', $nameEx);
}

echo $Name; // added missing semicolon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM