Agar PHP loop tugashi bilan tugaydi

Shaxsiy loyihada ishlash orqali PHP ni o'rganish. So'nggi 2-soat ichida nima uchun mening IF qoidam mening loopimda o'tkazib yuborilganini tushunishga harakat qilyapmanmi, men hozir tajribali kodlovchi menga biroz yordam berishi mumkin edi, chunki men hozirgi vaqtda haligacha yashayapman.

Nima qilmoqchiman

Foydalanuvchilar to'garakni to'ldirganda sport musobaqalarida ishtirok etish uchun tanlov qiladi.

Har bir narsa kutilgandek ishlaydi, mendan to'g'ri tanlovlar sonini hisoblash qiyin kechadi.

IMAGE1: THE CODE SHOULD PUT OUT THE FOLLOWING enter image description here

IMAGE2: KO'RIB ChIQARILGAN (YO'Q)

enter image description here

MY kodi

$sql ="SELECT picks.*, results.*, users.*
        FROM picks
        inner join results on picks.gameID = results.gameID
        inner join users on picks.userID = users.userID 
        WHERE picks.tournament = 'SixNations' AND picks.weekNum = '3'
        order by users.lastname, users.firstname";
        $stmnt = $db->prepare($sql);

        //JUST DISABLING FOR DEBUGING PURPOSES
        /*
        $stmnt->bindValue(':tournament', $tournament);
        $stmnt->bindValue(':weekNum', $round);*/
        $stmnt->execute()
        $picks = $stmnt->fetchAll();
        $totalCorrectPicks = 0;
        echo'<table class="table table-responsive table-striped">';
        echo'<thead>';
        echo'
        <tr>
        <th>FirstName</th>
        <th>Picked Team</th>
        <th>Winning Team</th>
        <th>Margin</th>
        <th>TOTAL CORRECT PICKS</th>
    </tr>
    </thead>
    <tbody>';
   $i = 1; //USED TO COUNT NR GAMES
        foreach($picks as $pick){
            $user = $pick['firstname'];
            $picked = $pick['selectedTeam'];
            $result = $pick['won'];
            $nrGames = $i;

            echo '<tr>';
            echo'<td>';
            echo $user;
            echo'</td>';
            echo'<td>';
            echo $pick['selectedTeam'];
            echo'</td>';
            echo'<td>';
            echo $pick['won'];
            echo'</td>';

        if($picked == $result){
            //USER PICKED CORRECT TEAM
            $totalCorrectPicks = $totalCorrectPicks +1;
                echo'<td>';
                    $margin = abs($pick['pts'] - $pick['points']);
                    echo $margin;
                echo'</td>';
        }//if
        else if($picked != $result){
            //user PICKED INCORRECT TEAM
            echo'<td>';
            $totalCorrectPicks += 0;
            $margin = 'WRONG PICK';
            echo $margin;
            echo'</td>';    
        }//else if
         ##THIS IF STATMENT IS GETTING IGNORED##
         if($user != $pick['firstname']){
             echo'<td>';
        echo $output = 'Total Correct Picks'. $totalCorrectPicks.'/'.$i; 
            echo'</td>';
            echo'</tr>';
         }//if   
          $i++; //keep track of number games  
      }//foreach
      echo'</tbody>';
      echo'</table>';

2-rasmda ko'rinib turganidek, yuqoridagi kod har bir foydalanuvchi tomonidan tanlangan jami to'g'ri o'yinni chop etadigan IF ifodasi atlanmoqda/e'tiborsizlanmoqda. Nima uchun va/yoki qanday yordam beraman? Bu juda xush kelibsiz.

1
@CynePhoba NO bu ishni qilmadi
qo'shib qo'ydi muallif Timothy Coetzee, manba
@CynePhoba NO bu ishni qilmadi
qo'shib qo'ydi muallif Timothy Coetzee, manba
@CynePhoba NO bu ishni qilmadi
qo'shib qo'ydi muallif Timothy Coetzee, manba
@CynePhoba NO bu ishni qilmadi
qo'shib qo'ydi muallif Timothy Coetzee, manba
Siz bu erda biroz g'alati ish qilyapsiz ... Dastlab $ user = $ tanlash ['firstname']; ni tanlang, keyin if ($ user! = $ Pick ['firstname ']) { - bu har doim noto'g'ri bo'ladi, chunki ular har doim tengdir.
qo'shib qo'ydi muallif Qirel, manba
Siz bu erda biroz g'alati ish qilyapsiz ... Dastlab $ user = $ tanlash ['firstname']; ni tanlang, keyin if ($ user! = $ Pick ['firstname ']) { - bu har doim noto'g'ri bo'ladi, chunki ular har doim tengdir.
qo'shib qo'ydi muallif Qirel, manba
Siz bu erda biroz g'alati ish qilyapsiz ... Dastlab $ user = $ tanlash ['firstname']; ni tanlang, keyin if ($ user! = $ Pick ['firstname ']) { - bu har doim noto'g'ri bo'ladi, chunki ular har doim tengdir.
qo'shib qo'ydi muallif Qirel, manba
Siz bu erda biroz g'alati ish qilyapsiz ... Dastlab $ user = $ tanlash ['firstname']; ni tanlang, keyin if ($ user! = $ Pick ['firstname ']) { - bu har doim noto'g'ri bo'ladi, chunki ular har doim tengdir.
qo'shib qo'ydi muallif Qirel, manba
echo $ output = 'Total Correct Picks'. $ totalCorrectPicks. '/<. $ i; Faqat buni amalga oshirib ko'ring echo "Total Correct Picks". $ totalCorrectPicks. '/'. $ i; ?
qo'shib qo'ydi muallif CynePhoba12, manba

7 javoblar

Senda bor:

foreach($picks as $pick){
    $user = $pick['firstname'];

So'ngra, keyin - bir xil foreach da siz buni tekshirib turasiz (bu sizning muammosiz):

if($user != $pick['firstname']){

Lekin $ user kodini $ user = $ tanlash ['firstname'] va if ($ user! $ Tanlash ['firstname']) iborasi.

Buning ma'nosi if ($ user! = $ Pick ["firstname"]) har doim atlanacaktır, chunki $ user = $ tanlash ["firstname"] pastadir boshlanishi.

Edit: In answer to your question - if I understand it correctly. You can simply do something like:

Birinchi foydalanuvchi nomini oling.

// Before the foreach call, need the 'first' user name.
$previousUser = $picks[0]['firstname'];

foreach($picks as $pick){

Mos kelmagan matchni tekshiring:

// For the statement check, where the "problem" is
if($previousUser != $pick['firstname'])

Va bu ko'chadan oxiriga qo'ying.

// At the end of the loop
$previousUser = $user;
$i++; //keep track of number games 
1
qo'shib qo'ydi
5 minutni tekshirish va amalga oshirish uchun sizga yo'lbarslarning minnatdorchiligini bildiraman, fikr-mulohaza bildiraman va qabul qilaman
qo'shib qo'ydi muallif Timothy Coetzee, manba
Xristian Tigger Sizning javobingiz uchun juda minnatdormiz, lekin menimcha, million dollarlik savol mening nomimdagi o'zgarishlarni ( $ user ) qanday qilib olishim va olishim kerakligini. .., amalga oshirish uchun if() uchun inorder kerakmi?
qo'shib qo'ydi muallif Timothy Coetzee, manba
Yangilanishni tahrirlash bo'limiga qarang. Men nima qilishni bilaman deb o'ylayman.
qo'shib qo'ydi muallif Tigger, manba

Senda bor:

foreach($picks as $pick){
    $user = $pick['firstname'];

So'ngra, keyin - bir xil foreach da siz buni tekshirib turasiz (bu sizning muammosiz):

if($user != $pick['firstname']){

Lekin $ user kodini $ user = $ tanlash ['firstname'] va if ($ user! $ Tanlash ['firstname']) iborasi.

Buning ma'nosi if ($ user! = $ Pick ["firstname"]) har doim atlanacaktır, chunki $ user = $ tanlash ["firstname"] pastadir boshlanishi.

Edit: In answer to your question - if I understand it correctly. You can simply do something like:

Birinchi foydalanuvchi nomini oling.

// Before the foreach call, need the 'first' user name.
$previousUser = $picks[0]['firstname'];

foreach($picks as $pick){

Mos kelmagan matchni tekshiring:

// For the statement check, where the "problem" is
if($previousUser != $pick['firstname'])

Va bu ko'chadan oxiriga qo'ying.

// At the end of the loop
$previousUser = $user;
$i++; //keep track of number games 
1
qo'shib qo'ydi
Xristian Tigger Sizning javobingiz uchun juda minnatdormiz, lekin menimcha, million dollarlik savol mening nomimdagi o'zgarishlarni ( $ user ) qanday qilib olishim va olishim kerakligini. .., amalga oshirish uchun if() uchun inorder kerakmi?
qo'shib qo'ydi muallif Timothy Coetzee, manba
5 minutni tekshirish va amalga oshirish uchun sizga yo'lbarslarning minnatdorchiligini bildiraman, fikr-mulohaza bildiraman va qabul qilaman
qo'shib qo'ydi muallif Timothy Coetzee, manba
Yangilanishni tahrirlash bo'limiga qarang. Men nima qilishni bilaman deb o'ylayman.
qo'shib qo'ydi muallif Tigger, manba

Senda bor:

foreach($picks as $pick){
    $user = $pick['firstname'];

So'ngra, keyin - bir xil foreach da siz buni tekshirib turasiz (bu sizning muammosiz):

if($user != $pick['firstname']){

Lekin $ user kodini $ user = $ tanlash ['firstname'] va if ($ user! $ Tanlash ['firstname']) iborasi.

Buning ma'nosi if ($ user! = $ Pick ["firstname"]) har doim atlanacaktır, chunki $ user = $ tanlash ["firstname"] pastadir boshlanishi.

Edit: In answer to your question - if I understand it correctly. You can simply do something like:

Birinchi foydalanuvchi nomini oling.

// Before the foreach call, need the 'first' user name.
$previousUser = $picks[0]['firstname'];

foreach($picks as $pick){

Mos kelmagan matchni tekshiring:

// For the statement check, where the "problem" is
if($previousUser != $pick['firstname'])

Va bu ko'chadan oxiriga qo'ying.

// At the end of the loop
$previousUser = $user;
$i++; //keep track of number games 
1
qo'shib qo'ydi
5 minutni tekshirish va amalga oshirish uchun sizga yo'lbarslarning minnatdorchiligini bildiraman, fikr-mulohaza bildiraman va qabul qilaman
qo'shib qo'ydi muallif Timothy Coetzee, manba
Xristian Tigger Sizning javobingiz uchun juda minnatdormiz, lekin menimcha, million dollarlik savol mening nomimdagi o'zgarishlarni ( $ user ) qanday qilib olishim va olishim kerakligini. .., amalga oshirish uchun if() uchun inorder kerakmi?
qo'shib qo'ydi muallif Timothy Coetzee, manba
Yangilanishni tahrirlash bo'limiga qarang. Men nima qilishni bilaman deb o'ylayman.
qo'shib qo'ydi muallif Tigger, manba

Senda bor:

foreach($picks as $pick){
    $user = $pick['firstname'];

So'ngra, keyin - bir xil foreach da siz buni tekshirib turasiz (bu sizning muammosiz):

if($user != $pick['firstname']){

Lekin $ user kodini $ user = $ tanlash ['firstname'] va if ($ user! $ Tanlash ['firstname']) iborasi.

Buning ma'nosi if ($ user! = $ Pick ["firstname"]) har doim atlanacaktır, chunki $ user = $ tanlash ["firstname"] pastadir boshlanishi.

Edit: In answer to your question - if I understand it correctly. You can simply do something like:

Birinchi foydalanuvchi nomini oling.

// Before the foreach call, need the 'first' user name.
$previousUser = $picks[0]['firstname'];

foreach($picks as $pick){

Mos kelmagan matchni tekshiring:

// For the statement check, where the "problem" is
if($previousUser != $pick['firstname'])

Va bu ko'chadan oxiriga qo'ying.

// At the end of the loop
$previousUser = $user;
$i++; //keep track of number games 
1
qo'shib qo'ydi
5 minutni tekshirish va amalga oshirish uchun sizga yo'lbarslarning minnatdorchiligini bildiraman, fikr-mulohaza bildiraman va qabul qilaman
qo'shib qo'ydi muallif Timothy Coetzee, manba
Xristian Tigger Sizning javobingiz uchun juda minnatdormiz, lekin menimcha, million dollarlik savol mening nomimdagi o'zgarishlarni ( $ user ) qanday qilib olishim va olishim kerakligini. .., amalga oshirish uchun if() uchun inorder kerakmi?
qo'shib qo'ydi muallif Timothy Coetzee, manba
Yangilanishni tahrirlash bo'limiga qarang. Men nima qilishni bilaman deb o'ylayman.
qo'shib qo'ydi muallif Tigger, manba

Umid qilamiz, bu yordam beradi:

KOD 1

// Assign Variable
$user = $pick['firstname'];
$nrGame = 1;

// Check user are the same from previous row or not
if($tempUser <> $user){
    $points = 0;
    $nrGame = 1;
}

// Pick correct team
if ($picked == $result){
    $points += 1;
}

// Last Column
echo "<td>".$points."/" .$nrGame."</td>";

// Assign temporary user
$tempUser = $user;

$nrGame++;

CODE 2 (Display Points on the last row only)

// Assign variable
$maxGame = 3;

// Last Column
if ($nrGame == $maxGame)
    echo "<td>".$points."/" .$nrGame."</td>";
else
    echo "<td></td>";
1
qo'shib qo'ydi
Siz juda ko'p Nyc2Xga eng yaxshi natijaga erishganingiz uchun minnatdorman, lekin men faqat bitta muammoga duch keldim, faqatgina so'nggi kolonnaning natijasi har bir foydalanuvchi ustunining oxirida ko'rsatilishini xohlayman. Iltimos, hozirgi va istalgan natijani ko'rish uchun imgur.com/a/XqvCg
qo'shib qo'ydi muallif Timothy Coetzee, manba
Ha, bitta muammo - o'yinlarning soni o'zgarishi mumkin. Ehtimol, buni count() so'rovini mening sql statumentimga qo'shish orqali hal qila olaman
qo'shib qo'ydi muallif Timothy Coetzee, manba
@TimothyCoetzee Buning uchun o'ylaymanki, siz o'yinni e'lon qilishingiz kerak.
qo'shib qo'ydi muallif Nyc2x, manba
Ha, sayg'oqlarni() ishlating.
qo'shib qo'ydi muallif Nyc2x, manba

Umid qilamiz, bu yordam beradi:

KOD 1

// Assign Variable
$user = $pick['firstname'];
$nrGame = 1;

// Check user are the same from previous row or not
if($tempUser <> $user){
    $points = 0;
    $nrGame = 1;
}

// Pick correct team
if ($picked == $result){
    $points += 1;
}

// Last Column
echo "<td>".$points."/" .$nrGame."</td>";

// Assign temporary user
$tempUser = $user;

$nrGame++;

CODE 2 (Display Points on the last row only)

// Assign variable
$maxGame = 3;

// Last Column
if ($nrGame == $maxGame)
    echo "<td>".$points."/" .$nrGame."</td>";
else
    echo "<td></td>";
1
qo'shib qo'ydi
Siz juda ko'p Nyc2Xga eng yaxshi natijaga erishganingiz uchun minnatdorman, lekin men faqat bitta muammoga duch keldim, faqatgina so'nggi kolonnaning natijasi har bir foydalanuvchi ustunining oxirida ko'rsatilishini xohlayman. Iltimos, hozirgi va istalgan natijani ko'rish uchun imgur.com/a/XqvCg
qo'shib qo'ydi muallif Timothy Coetzee, manba
Ha, bitta muammo - o'yinlarning soni o'zgarishi mumkin. Ehtimol, buni count() so'rovini mening sql statumentimga qo'shish orqali hal qila olaman
qo'shib qo'ydi muallif Timothy Coetzee, manba
@TimothyCoetzee Buning uchun o'ylaymanki, siz o'yinni e'lon qilishingiz kerak.
qo'shib qo'ydi muallif Nyc2x, manba
Ha, sayg'oqlarni() ishlating.
qo'shib qo'ydi muallif Nyc2x, manba

Umid qilamiz, bu yordam beradi:

KOD 1

// Assign Variable
$user = $pick['firstname'];
$nrGame = 1;

// Check user are the same from previous row or not
if($tempUser <> $user){
    $points = 0;
    $nrGame = 1;
}

// Pick correct team
if ($picked == $result){
    $points += 1;
}

// Last Column
echo "<td>".$points."/" .$nrGame."</td>";

// Assign temporary user
$tempUser = $user;

$nrGame++;

CODE 2 (Display Points on the last row only)

// Assign variable
$maxGame = 3;

// Last Column
if ($nrGame == $maxGame)
    echo "<td>".$points."/" .$nrGame."</td>";
else
    echo "<td></td>";
1
qo'shib qo'ydi
Siz juda ko'p Nyc2Xga eng yaxshi natijaga erishganingiz uchun minnatdorman, lekin men faqat bitta muammoga duch keldim, faqatgina so'nggi kolonnaning natijasi har bir foydalanuvchi ustunining oxirida ko'rsatilishini xohlayman. Iltimos, hozirgi va istalgan natijani ko'rish uchun imgur.com/a/XqvCg
qo'shib qo'ydi muallif Timothy Coetzee, manba
Ha, bitta muammo - o'yinlarning soni o'zgarishi mumkin. Ehtimol, buni count() so'rovini mening sql statumentimga qo'shish orqali hal qila olaman
qo'shib qo'ydi muallif Timothy Coetzee, manba
@TimothyCoetzee Buning uchun o'ylaymanki, siz o'yinni e'lon qilishingiz kerak.
qo'shib qo'ydi muallif Nyc2x, manba
Ha, sayg'oqlarni() ishlating.
qo'shib qo'ydi muallif Nyc2x, manba
PhP |BotsUz
PhP |BotsUz
93 ishtirokchilar

Phpni o'rganishni Hohlasangiz https://t.me/joinchat/AAAAAE-KRc5dd5tPMmGmWA A'zo bo'lin