因此,我正在嘗試爲我的網站創建分頁,而無需刷新頁面。 我爲此使用了ajax,php和mysql。PHP連接無法正常工作
目前,我被困在PHP,特別是與foreach和concatenation。
這裏是我的foreach循環:
if (isset($_POST['page'])) {
$algus = $_POST['page'];
$loos = '';
$mitu = 5;
$msg = [];
foreach (uritused($conn, $id, $algus, $mitu) as $key => $vals) {
$loos .= "<tr class='tr-data'>";
$loos .= '<td>'.$vals['eventi_nimi'].'<br><span class="owner" data-id="'.$vals['owner-id'].'">'.$vals['owner'].'</span></td>';
$loos .= '<td data-loosiaeg="'.$vals['loosiAeg'].'">'.$vals['loosiAeg'].'</td>';
$loos .= '<td>'.$vals['attendiloos'].'</td>';
$loos .= '<td>';
if (is_array($vals['auhinnad'])) {
foreach($vals['auhinnad'] as $keys => $val){
$loos .= $val['auhind'].'<br>';
}
} else {
$loos .= $vals['auhinnad'];
}
$loos .= '</td>';
$loos .= '<td>Postitus: '.$vals['tingimused']['postitus'].'<br>Osalus: '.$vals['tingimused']['osalus'].'</td>';
$loos .= '<td>';
if (is_array($vals['valista'])) {
foreach($vals['valista'] as $keys => $val){
$loos .= $val['nimi'].'<br>';
}
} else {
$loos .= $vals['valista'];
}
$loos .= '</td>';
$loos .= '<td data-sec='.$vals['sec'].' class="tr-icons">
<i class="material-icons staatus ' . $vals['staatus'] . '" data-staatus="'.$vals['staatus'].'" data-toggle="tooltip" data-placement="bottom" data-class="stopped" data-eltext="radio_button_checked" data-text="radio_button_unchecked" title="Peata loosimine">radio_button_checked</i></td>';
$loos .= "</tr>";
}
$msg['data'] = $loos;
$msg['success'] = true;
echo json_encode($msg);
}
這裏是我的Ajax調用
var page = 1;
$.ajax({
type: "POST",
url: "pages/php/loosid_data.php",
cache: false,
dataType: 'json',
data: {'page': page},
success: function(msg){
console.log(msg);
if(msg['success']){
$('.tr-data').remove();
$('.m-loosid table').append(msg['data']);
}
},
error: function(msg){
console.log(msg);
}
});
編輯:繼承人的功能
function uritused($conn, $id, $algus, $mitu){
try{
$uritused = $conn->prepare("SELECT COUNT(*) FROM loosid WHERE usr_id='$id'");
$uritused->execute();
}catch(PDOException $e){
echo $e->getMessage();
}
$uritus_arr = [];
$count = $uritused->fetchColumn();
if($count > 0){
try{
$urituseds = $conn->prepare("SELECT * FROM loosid WHERE usr_id='176237127636' ORDER BY loosiAeg DESC LIMIT 1, 5");
$urituseds->execute();
}catch(PDOException $e){
echo $e->getMessage();
}
$uritus = $urituseds->fetchAll();
for ($z=0;$z<$count;$z++) {
$uritus_arr[$z] = array(
'eventi_nimi' => $uritus[$z]['eventi_nimi'],
'loosiAeg' => $uritus[$z]['loosiAeg'],
'tingimused' => array(
'postitus' => $uritus[$z]['post'],
'osalus' => $uritus[$z]['osalus']
),
'sec' => $uritus[$z]['sec'],
'staatus' => $uritus[$z]['staatus'],
'attendiloos' => $uritus[$z]['attendiloos']
);
$auhinnad = explode(',', $uritus[$z]['auhinnad']);
if($uritus[$z]['auhinnad'] != '-'){
foreach ($auhinnad as $key) {
$auhind = explode('-', $key);
$auhinna_arr[] = array(
'auhind' => $auhind[0],
'voitjad' => $auhind[1]
);
}
$uritus_arr[$z]['auhinnad'] = $auhinna_arr;
}else{
$uritus_arr[$z]['auhinnad'] = $uritus[$z]['auhinnad'];
}
$valista = explode(',', $uritus[$z]['valista']);
if($uritus[$z]['valista'] != '-'){
foreach ($valista as $keys) {
$valistad = explode('-', $keys);
$valista_arr[] = array(
'nimi' => $valistad[0],
'id' => $valistad[1]
);
}
$uritus_arr[$z]['valista'] = $valista_arr;
}else{
$uritus_arr[$z]['valista'] = $uritus[$z]['valista'];
}
$valista_arr = [];
$auhinna_arr = [];
}
}
return $uritus_arr;
}
現在,這給了我UNDEFINED OFFSET,雖然我知道數據庫中有這些信息。 由於某些原因數據總是空的。
每當我嘗試在沒有ajax的情況下直接在我的頁面中運行這個相同的代碼時,一切都按照它應該的方式工作。
也許你們對如何解決這個問題有什麼建議?我知道這裏有同樣的帖子,但我還沒有找到我的答案。
顯示uritused函數的代碼 –
看起來像'dataType:'json','但發送'html'作爲迴應 – RamRaider
你確定'uritused'返回任何東西嗎? –