简体   繁体   中英

How do I code a loop for my echo statement?

I get only one printed result in the foreach echo loop at the bottom of the page.

<?php
    defined('_JEXEC') or die('Restricted access');
    $db =& JFactory::getDBO();

    $query0 = "SELECT * FROM  `jos_ginfo` WHERE . . . LIMIT 30";

    //echo $query0;
    $db->setQuery($query0);
    $ginfo = $db->loadObjectList();
    //echo
    //$ginfo[0];
    foreach($ginfo as $ginfo[$i]):
    {$i=0; $i++;} 
     endforeach;
    echo $db->getErrorMsg();

    if(empty($ginfo)){
    echo "<center>No
    game found, try a different entry.</center>";
    }else{


    $pgndata = array ( $ginfo[$i]->Id);

        $i=0;
    foreach($pgndata as $ginfo[$i]->Id):
    //I am only getting one printed result!
    {
        echo "<a href='/index.php?option=com_publishpgn&tactical-game=".$ginfo[$i]->Id."&Itemid=78.html'>\n";
    echo "".$ginfo[$i]->White." v. ".$ginfo[$i]->Black."  (".$ginfo[$i]->Result.") ".$ginfo[$i]->EventDate." ECO:".$ginfo[$i]->ECO."</a><br>\n";

    $i++;
    }

     endforeach;

    //echo "</div>";
        }
    ?>

I suppose, you need this:

$ginfo = $db->loadObjectList();
foreach($ginfo as $value)
{
    echo $value . '<br />';
}

Further, see foreach manual and other loops.

This is a modified version of Sarfraz's code.

Try this..

// Array of multiple games
$ginfo = $db->loadObjectList();

// Loop through games array
foreach ($ginfo as $index => $singleGameInfo)
{
    foreach($singleGameInfo as $elementName => $elementValue)
    {
        echo "[$elementName \"$elementValue\"]\n";
    }
}

In place of...

echo "[Event \"".$ginfo[0]->Event."\"]\n";

echo "[Site \"".$ginfo[0]->Site."\"]\n";

echo "[Date \"".$ginfo[0]->Date."\"]\n";

echo "[Round \"".$ginfo[0]->Round."\"]\n";

echo "[White \"".$ginfo[0]->White."\"]\n";

echo "[Black \"".$ginfo[0]->Black."\"]\n";

echo "[Result \"".$ginfo[0]->Result."\"]\n";

echo "[ECO \"".$ginfo[0]->ECO."\"]\n";

echo "[WhiteElo \"".$ginfo[0]->WhiteElo."\"]\n";

echo "[BlackElo \"".$ginfo[0]->BlackElo."\"]\n";

echo "[Annotator \"".$ginfo[0]->Annotator."\"]\n";

echo "[SetUp \"".$ginfo[0]->SetUp."\"]\n";

Edit: Are you trying to loop through multiple games or the field-data of a single game?

Edit2: Updated to loop through games

$query = mysql_query("select * from table ");

while ($result = mysql_fetch_array($query)) { echo "$result[id]"; echo "$result[firstname]";

}

A few errors in your code:

foreach($ginfo as $ginfo[$i]):
    {$i=0; $i++;} 

On the first iteration, $i is undefined, so the first value pulled from $ginfo for the foreach loop will be stored in $ginfo[null] . You then set $i to 0, increment it, and loop around, so now the next value gets stored in $ginfo[1] , as will all further iterations. So you end up with only two values extracted from the $ginfo object, and stored in the 'null' and '1' keys.

Later, you do

$pgndata = array ( $ginfo[$i]->Id);

You're not doing this inside a loop, so $pgndata becomes an array with a single element taken from $ginfo[1]->Id . You then immediately do

foreach($pgndata as $ginfo[$i]->Id):

but $pgndata has only a single element in it, which explains why you only have one item output.

I don't know what your ->loadObjectList() at the top does. Is it returning an array? An object? If it's any array, what's the point of the first foreach loop? You're destroying all but the first two values present in it.

It's never ever a good idea to try and modify an array WHILE you're looping over it in a foreach loop. It's kinda like trying to change a tire and axle and transmission on your car while going down the highway at 100mph. You might get lucky once, the rest of the time you're going to be spread into a thin ketchup stain.

Also, why are you mixing the {} and : / end syntaxes? Choose one or the other, but don't use both. Braces are standard and universally understood. the :/end version is far less popular and unfamiliar to most people.

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