09/10/2018, 23:44

Giúp em về phép lặp while trong PHP

Sau khi truy xuất từ CSDL mySQL, em dùng phép lặp như sau

while ($row = $db->sql_fetchrow($result)) {
$catid = intval($row['catid']);
$parentid = $row['parentid'];
$title = $row['title'];
$boxstuff .= "<div class="content" onclick="SwitchMenu('sub$i')">$title</div> ";
Giờ em muốn mỗi lần lặp thì $i sẽ tăng thêm 1 đơn vị, các bác giúp em với đc ko ạ
anhtuannd viết 02:00 ngày 10/10/2018
Cuối vòng lặp thêm vào cái $i++;
Tưởng cái này là vô cùng cơ bản rồi chứ?
germini viết 01:49 ngày 10/10/2018
$i=1;
while ($row = $db->sql_fetchrow($result)) {
$catid = intval($row['catid']);
$parentid = $row['parentid'];
$title = $row['title'];
$boxstuff .= "<div class=\"content\" onclick=\"SwitchMenu('sub$i')\">$title</div>\n";
$i++;
Thế này có được không nhĩ, nếu sai mấy bác thông cảm, em cũng mù php
vnmarser viết 01:48 ngày 10/10/2018
không được
1. Thiếu cái } ở dòng cuối.
2. Nên đặt $i trong while{}
minhhuy001122 viết 01:51 ngày 10/10/2018
Code:
$i = 1;
while ($row = $db->sql_fetchrow($result)) {
$catid = intval($row['catid']);
$parentid = $row['parentid'];
$title = $row['title'];
$boxstuff .= "<div class=\"content\" onclick=\"SwitchMenu('sub$i')\">$title</div>\n";
$i++;
}
tôi thật tình là không hiểu tại sao có rất nhiều bạn lập trình php lại rất khoái sài biến tạm nhỉ , đây là 1 sai lầm khá nghiêm trọng đó .
Code:
$title = $row['title'];
$boxstuff .= "<div class=\"content\" onclick=\"SwitchMenu('sub$i')\">$title</div>\n";
sao không là như vầy luôn
Code:
$boxstuff .= "<div class=\"content\" onclick=\"SwitchMenu('sub$i')\">$row['title']</div>\n";
lnt viết 01:58 ngày 10/10/2018
Quote Được gửi bởi minhhuy001122 View Post
Code:
$i = 1;
while ($row = $db->sql_fetchrow($result)) {
$catid = intval($row['catid']);
$parentid = $row['parentid'];
$title = $row['title'];
$boxstuff .= "<div class=\"content\" onclick=\"SwitchMenu('sub$i')\">$title</div>\n";
$i++;
}
tôi thật tình là không hiểu tại sao có rất nhiều bạn lập trình php lại rất khoái sài biến tạm nhỉ , đây là 1 sai lầm khá nghiêm trọng đó .
Code:
$title = $row['title'];
$boxstuff .= "<div class=\"content\" onclick=\"SwitchMenu('sub$i')\">$title</div>\n";
sao không là như vầy luôn
Code:
$boxstuff .= "<div class=\"content\" onclick=\"SwitchMenu('sub$i')\">$row['title']</div>\n";
Dùng biến tạm không có gì sai cả. Cẩn thận thì unset nó trước khi dùng.
amida viết 01:46 ngày 10/10/2018
Quote Được gửi bởi minhhuy001122 View Post
Code:
$i = 1;
while ($row = $db->sql_fetchrow($result)) {
$catid = intval($row['catid']);
$parentid = $row['parentid'];
$title = $row['title'];
$boxstuff .= "<div class=\"content\" onclick=\"SwitchMenu('sub$i')\">$title</div>\n";
$i++;
}
tôi thật tình là không hiểu tại sao có rất nhiều bạn lập trình php lại rất khoái sài biến tạm nhỉ , đây là 1 sai lầm khá nghiêm trọng đó .
Code:
$title = $row['title'];
$boxstuff .= "<div class=\"content\" onclick=\"SwitchMenu('sub$i')\">$title</div>\n";
sao không là như vầy luôn
Code:
$boxstuff .= "<div class=\"content\" onclick=\"SwitchMenu('sub$i')\">$row['title']</div>\n";
Vậy xin cho hỏi nếu với đoạn code này thì sao
Code:
  $boxstuff .= "<div class=\"content\" onclick=\"SwitchMenu('sub$i')\">$row['title'].substr($row['title'],0,n)</div>\n";
lnt viết 01:57 ngày 10/10/2018
Quote Được gửi bởi amida View Post
Vậy xin cho hỏi nếu với đoạn code này thì sao
Code:
  $boxstuff .= "<div class=\"content\" onclick=\"SwitchMenu('sub$i')\">$row['title'].substr($row['title'],0,n)</div>\n";
Code:
$boxstuff .= "<div class=\"content\" onclick=\"SwitchMenu('sub$i')\">$row[title]".substr($row['title'],0,n)."</div>\n";
Hàm không thể lồng trong chuỗi.
amida viết 01:46 ngày 10/10/2018
Quote Được gửi bởi lnt View Post
Code:
$boxstuff .= "<div class=\"content\" onclick=\"SwitchMenu('sub$i')\">$row[title]".substr($row['title'],0,n)."</div>\n";
Hàm không thể lồng trong chuỗi.
Sorry,tôi nhầm thành echo
echo "<div class=\"content\" onclick=\"SwitchMenu('sub$i')\">$row[title]".substr($row['title'],0,n)."</div>\n";
Bài liên quan
0