如何使用javascript根据房间可用性进行提示?

发布于 2024-09-27 06:10:56 字数 517 浏览 7 评论 0原文

我是编程新手,我需要有关我的代码的帮助。我希望我的页面能够提示我是否还有可用房间。我正在使用管理页面上的 onload 功能。

到目前为止,这是我的代码

function prompt()
{
< ?php 
include("dbconfig.php");

$sql = "SELECT COUNT(*) FROM rooms WHERE status = 'available'";
$result = @mysql_query($sql) or die("Could not execute query");

?> 
if(< ?php $result <= 14 ?>){
 alert("Rooms left: < ?php echo $result ?>");
 }

else{
 alert("Welcome Admin.");
 } 
}

window.onload=prompt;

编辑:

代码现在工作正常,但它显示“资源 id#4”,而不是计数的值。

I'm new to programming and I need help on my code. I want my page to prompt me if there will be available rooms left. I'm using the onload function on the admin page.

so far here is my code

function prompt()
{
< ?php 
include("dbconfig.php");

$sql = "SELECT COUNT(*) FROM rooms WHERE status = 'available'";
$result = @mysql_query($sql) or die("Could not execute query");

?> 
if(< ?php $result <= 14 ?>){
 alert("Rooms left: < ?php echo $result ?>");
 }

else{
 alert("Welcome Admin.");
 } 
}

window.onload=prompt;

edit:

The code worked fine now but it displays "Resource id#4", not the value of the count.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤单情人 2024-10-04 06:10:56

我觉得你不能将 php 与 js 代码混合在一起。
php主要是服务端,js是客户端
从您提供的代码片段来看,也许您应该使用纯粹的 php,如下所示:

< ?php 
    include("dbconfig.php");
    $sql = "SELECT COUNT(*) FROM rooms WHERE status = 'available'"; 
    $result = @mysql_query($sql) or die("Could not execute query");
    if ($result <= 14) {
        echo("Rooms left: $result");
    }
    else {
        echo("Welcome Admin.")
    }
 ?>

这应该在请求时首先运行

I feel you can't mix php with js codes.
php is mainly on server side , while the js is client side
from the snippet you provide, maybe you should use purely php as follows:

< ?php 
    include("dbconfig.php");
    $sql = "SELECT COUNT(*) FROM rooms WHERE status = 'available'"; 
    $result = @mysql_query($sql) or die("Could not execute query");
    if ($result <= 14) {
        echo("Rooms left: $result");
    }
    else {
        echo("Welcome Admin.")
    }
 ?>

This should be run at the first when request

删除会话 2024-10-04 06:10:56

我认为您对 PHP 处理位置与 Javascript 处理位置感到困惑。

PHP 在服务器端处理,而Javascript 在客户端处理。可以这样想……

  1. 您访问一个页面。
  2. 您的 PHP 被处理,最终输出被发送到浏览器。
  3. 您的 Javascript 由浏览器处理。

正如你现在所拥有的,你会得到一些有趣的输出......特别是因为你缺乏 echo 语句。以下是您可能在浏览器页面源代码中看到的内容:

function prompt()
{
if(){
 alert("Rooms left: < ?php echo $result ?>");
 }
else{
 alert("Welcome Admin.");
 } 
}

window.onload=prompt;

注意空的 if 语句(还有开始标记中的空格:

if(<?php echo ($result <= 14); ?>){
 alert("Rooms left: <?php echo $result ?>");
 }

这应该使您的 Javascript 评估布尔值 true/false。不要忘记 Javascript 需要用 < 包裹起来。脚本>也有标签!

回答您的 MySQL 问题...
试试这样:

//We can alias the COUNT(*) as MyCount for easy reference
$sql = "SELECT COUNT(*) as MyCount FROM rooms WHERE status = 'available'";
$result = @mysql_query($sql) or die("Could not execute query");
$row = mysql_fetch_array($result); //$row is now an array and will now have your count in it
echo $row['MyCount']; //This will print the count from the database. You could use it in other ways as well.

I think you are confused about where PHP processes vs. where Javascript processes.

PHP is processed on the server side, while Javascript is processed on the client side. Think of it like this...

  1. You access a page.
  2. Your PHP is processed, and the final output is sent to the browser.
  3. Your Javascript is processed by the browser.

As you have it now, you'd be getting some funny output... especially because of your lack of echo statements. Here is what you'd probably be seeing in your browser page source:

function prompt()
{
if(){
 alert("Rooms left: < ?php echo $result ?>");
 }
else{
 alert("Welcome Admin.");
 } 
}

window.onload=prompt;

Notice the empty if statement (also the space in the start tags:

if(<?php echo ($result <= 14); ?>){
 alert("Rooms left: <?php echo $result ?>");
 }

This should make your Javascript evaluate a boolean true/false. Don't forget that Javascript needs to be wrapped in a < script > tag too!

To answer your MySQL question...
Try it like this:

//We can alias the COUNT(*) as MyCount for easy reference
$sql = "SELECT COUNT(*) as MyCount FROM rooms WHERE status = 'available'";
$result = @mysql_query($sql) or die("Could not execute query");
$row = mysql_fetch_array($result); //$row is now an array and will now have your count in it
echo $row['MyCount']; //This will print the count from the database. You could use it in other ways as well.
无人问我粥可暖 2024-10-04 06:10:56

mysql_query 返回资源,而不是结果。
尝试使用:

$sql = "SELECT COUNT(*) FROM `rooms` WHERE `status` = 'available'";
$res = @mysql_query($sql) or die('Could not execute query');
$count = mysql_result($res, 0, 0);

mysql_query returns resource, not a result.
Try to use:

$sql = "SELECT COUNT(*) FROM `rooms` WHERE `status` = 'available'";
$res = @mysql_query($sql) or die('Could not execute query');
$count = mysql_result($res, 0, 0);
卷耳 2024-10-04 06:10:56

php 标签中不应有空格:

< ?php 
 ^

应该是:

<?php 

您还缺少 fetching 函数,以下是如何获取变量中的行数:

<?php $count = mysql_num_rows($result);?>

稍后您可以使用 $count< if 条件中的 /code> 变量。

There should be no space in php tags:

< ?php 
 ^

Should be:

<?php 

You are also missing a fetching function, here is how you can get row count in a variable:

<?php $count = mysql_num_rows($result);?>

Later you can use the $count variable in the if condition.

末骤雨初歇 2024-10-04 06:10:56

使用 mysql_fetch_row ,然后在条件中进行比较到$row[0]

$sql = "SELECT COUNT(*) FROM `rooms` WHERE `status` = 'available'";
$res = @mysql_query($sql) or die('Could not execute query');

$row = mysql_fetch_row($res);

if(< ?php $row[0] <= 14 ?>){

use mysql_fetch_row , and after that in the condition , compare to $row[0]

$sql = "SELECT COUNT(*) FROM `rooms` WHERE `status` = 'available'";
$res = @mysql_query($sql) or die('Could not execute query');

$row = mysql_fetch_row($res);

if(< ?php $row[0] <= 14 ?>){
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文