Home >Backend Development >PHP Tutorial >javascript - PHP and Ajax do paging jump, but it can't come out. I hope God can take a look
I want to make an ajax partial refresh paging jump, but I don’t know why the data received by ajax is always displayed on the page
like this: MySql ErrorNo database selected Then I did a few tests
I found echo "111111"; echo "22222";echo"3333"echo"4444";Only 4444 is output! I don’t know PHP very well, so I want to trouble the experts to find out what’s going on! I would like to thank you for this!
<code><meta http-equiv="Content-Type" content="text/html; charset=utf8" /> <?php if($_REQUEST['page']) { $page = $_REQUEST['page']; $page=1; $cur_page = $page; $page -= 1; $per_page = 10; $previous_btn = true; $next_btn = true; $first_btn = true; $last_btn = true; $start = $page * $per_page; $conn=mysqli_connect("localhost","root","","info"); mysqli_query($conn,"SET NAMES 'utf8'"); **echo "44444444";** $query_pag_data = "SELECT id,name from content LIMIT $start, $per_page"; $result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error()); $msg = ""; while ($row = mysql_fetch_array($result_pag_data)) { $htmlmsg=htmlspecialchars($row['name']); $msg .= "<li><b>" . $row['id'] . "</b> " . $htmlmsg . "</li>"; } $msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data **echo "3333333333333333333333";** /* --------------------------------------------- */ $query_pag_num = "SELECT COUNT(*) AS count FROM content"; $result_pag_num = mysql_query($query_pag_num); $row = mysql_fetch_array($result_pag_num); $count = $row['count']; $no_of_paginations = ceil($count / $per_page); **echo "2222222222";** /* ---------------Calculating the starting and endign values for the loop----------------------------------- */ if ($cur_page >= 7) { $start_loop = $cur_page - 3; if ($no_of_paginations > $cur_page + 3) $end_loop = $cur_page + 3; else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) { $start_loop = $no_of_paginations - 6; $end_loop = $no_of_paginations; } else { $end_loop = $no_of_paginations; } } else { $start_loop = 1; if ($no_of_paginations > 7) $end_loop = 7; else $end_loop = $no_of_paginations; } /* ----------------------------------------------------------------------------------------------------------- */ $msg .= "<div class='pagination'><ul>"; // FOR ENABLING THE FIRST BUTTON if ($first_btn && $cur_page > 1) { $msg .= "<li p='1' class='active'>First</li>"; } else if ($first_btn) { $msg .= "<li p='1' class='inactive'>First</li>"; } // FOR ENABLING THE PREVIOUS BUTTON if ($previous_btn && $cur_page > 1) { $pre = $cur_page - 1; $msg .= "<li p='$pre' class='active'>Previous</li>"; } else if ($previous_btn) { $msg .= "<li class='inactive'>Previous</li>"; } for ($i = $start_loop; $i <= $end_loop; $i++) { if ($cur_page == $i) $msg .= "<li p='$i' style='color:#fff;background-color:#006699;' class='active'>{$i}</li>"; else $msg .= "<li p='$i' class='active'>{$i}</li>"; } // TO ENABLE THE NEXT BUTTON if ($next_btn && $cur_page < $no_of_paginations) { $nex = $cur_page + 1; $msg .= "<li p='$nex' class='active'>Next</li>"; } else if ($next_btn) { $msg .= "<li class='inactive'>Next</li>"; } // TO ENABLE THE END BUTTON if ($last_btn && $cur_page < $no_of_paginations) { $msg .= "<li p='$no_of_paginations' class='active'>Last</li>"; } else if ($last_btn) { $msg .= "<li p='$no_of_paginations' class='inactive'>Last</li>"; } $goto = "<input type='text' class='goto' size='1' style='margin-top:-1px;margin-left:60px;'/><input type='button' id='go_btn' class='go_button' value='Go'/>"; $total_string = "<span class='total' a='$no_of_paginations'>Page <b>" . $cur_page . "</b> of <b>$no_of_paginations</b></span>"; $msg = $msg . "</ul>" . $goto . $total_string . "</div>"; // Content for pagination **echo $msg;** **echo "1111111111111";** } ?> </code>
like this: MySql ErrorNo database selected Then I did a few tests
I found echo "111111"; echo "22222";echo"3333"echo"4444";Only 4444 is output! I don’t know PHP very well, so I want to trouble the experts to find out what’s going on! I would like to thank you for this!
<code><meta http-equiv="Content-Type" content="text/html; charset=utf8" /> <?php if($_REQUEST['page']) { $page = $_REQUEST['page']; $page=1; $cur_page = $page; $page -= 1; $per_page = 10; $previous_btn = true; $next_btn = true; $first_btn = true; $last_btn = true; $start = $page * $per_page; $conn=mysqli_connect("localhost","root","","info"); mysqli_query($conn,"SET NAMES 'utf8'"); **echo "44444444";** $query_pag_data = "SELECT id,name from content LIMIT $start, $per_page"; $result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error()); $msg = ""; while ($row = mysql_fetch_array($result_pag_data)) { $htmlmsg=htmlspecialchars($row['name']); $msg .= "<li><b>" . $row['id'] . "</b> " . $htmlmsg . "</li>"; } $msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data **echo "3333333333333333333333";** /* --------------------------------------------- */ $query_pag_num = "SELECT COUNT(*) AS count FROM content"; $result_pag_num = mysql_query($query_pag_num); $row = mysql_fetch_array($result_pag_num); $count = $row['count']; $no_of_paginations = ceil($count / $per_page); **echo "2222222222";** /* ---------------Calculating the starting and endign values for the loop----------------------------------- */ if ($cur_page >= 7) { $start_loop = $cur_page - 3; if ($no_of_paginations > $cur_page + 3) $end_loop = $cur_page + 3; else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) { $start_loop = $no_of_paginations - 6; $end_loop = $no_of_paginations; } else { $end_loop = $no_of_paginations; } } else { $start_loop = 1; if ($no_of_paginations > 7) $end_loop = 7; else $end_loop = $no_of_paginations; } /* ----------------------------------------------------------------------------------------------------------- */ $msg .= "<div class='pagination'><ul>"; // FOR ENABLING THE FIRST BUTTON if ($first_btn && $cur_page > 1) { $msg .= "<li p='1' class='active'>First</li>"; } else if ($first_btn) { $msg .= "<li p='1' class='inactive'>First</li>"; } // FOR ENABLING THE PREVIOUS BUTTON if ($previous_btn && $cur_page > 1) { $pre = $cur_page - 1; $msg .= "<li p='$pre' class='active'>Previous</li>"; } else if ($previous_btn) { $msg .= "<li class='inactive'>Previous</li>"; } for ($i = $start_loop; $i <= $end_loop; $i++) { if ($cur_page == $i) $msg .= "<li p='$i' style='color:#fff;background-color:#006699;' class='active'>{$i}</li>"; else $msg .= "<li p='$i' class='active'>{$i}</li>"; } // TO ENABLE THE NEXT BUTTON if ($next_btn && $cur_page < $no_of_paginations) { $nex = $cur_page + 1; $msg .= "<li p='$nex' class='active'>Next</li>"; } else if ($next_btn) { $msg .= "<li class='inactive'>Next</li>"; } // TO ENABLE THE END BUTTON if ($last_btn && $cur_page < $no_of_paginations) { $msg .= "<li p='$no_of_paginations' class='active'>Last</li>"; } else if ($last_btn) { $msg .= "<li p='$no_of_paginations' class='inactive'>Last</li>"; } $goto = "<input type='text' class='goto' size='1' style='margin-top:-1px;margin-left:60px;'/><input type='button' id='go_btn' class='go_button' value='Go'/>"; $total_string = "<span class='total' a='$no_of_paginations'>Page <b>" . $cur_page . "</b> of <b>$no_of_paginations</b></span>"; $msg = $msg . "</ul>" . $goto . $total_string . "</div>"; // Content for pagination **echo $msg;** **echo "1111111111111";** } ?> </code>The above is the code
Look at the document, boy, click on the link
Since you are using mysqli, the third floor is right, select the database, mysqli ismysqli_select_db()
This is in the link, take a look.
Mysql and mysqli can be used together!
Used above
$conn=mysqli_connect("localhost","root","","info");Used below
mysql_query($query_pag_data)
How about changing it to
<code class="php">$conn=mysql_connect("localhost","root","","info"); mysql_query($conn,"SET NAMES 'utf8'");</code>
This error is because the database is not selected. You can execute
mysqli_query('use database')
after connecting. database
is your database. In addition, as mentioned above, don’t mix mysqli and mysql. PHP official I have given up on extending mysql
and use pdo
or mysqli