dqkxo44488 2011-07-22 19:38
浏览 54
已采纳

SQL数据库到php网站用javascript

I seem to be stuck on a concept in database query and website development.

I have a website that will reflect what data is stored in a database and the website needs to change depending on that data: therefore, my menu system will not be hardcoded in. It builds my menu system based off a query of all the models in my database. The action of clicking on the menu will show tables without changing the page (a simple javascript "showtables" function). like so:

        function showTables(TABLE_NAME)
{
    if(TABLE_NAME != "PRINTER_TABLE")
    {
        document.getElementById("PRINTER_TABLE").style.display ="none";

    }
    if(TABLE_NAME != "show_ALL_PRINTERS")
    {
        document.getElementById("show_ALL_PRINTERS").style.display ="none";
    }
    document.getElementById(TABLE_NAME).style.display ="block";
}

I did not include all of my other if statements because there are about 15 of them. These statements will hide everything and the only show the formatted table of "TABLE_NAME" at the end of that script.

My problem is that, if all of the data will not be hardcoded in either HTML or PHP, I need to pass into my function "showTables" a model type or ID that will come from my query.

My Menu system code snipet:

     <li class="hasmore"><a href="#" onClick="showTables('show_ALL_PRINTERS')"><span>Printer Parts</span></a>
      <ul class="dropdown">
      <?php
        include 'connection.php';

        $query = "SELECT * FROM all_printers";
        $result = mysql_query($query);
        while($row = mysql_fetch_array($result))
        {
            #echo "<h3>" . $row['printer_model'] . "</h3>";
            echo "<li><a href=\"#\" onclick=\"showTables('PRINTER_TABLE'")\">" .$row['printer_model']."</a></li>
";
        }
    ?>

This puts the model into the menu system and the "PRINTER_TABLE" will access my showTables function, which then will show that table. But there are many different printer models and I need to tell my table query what specific model to get info on.

I hope this makes sense as there is a lot of logic behind it. Maybe there is an easier way..?

In my tables I have:

    <table id="table">
    <thead>
          <tr>
              <th scope="col" id="table">Type</th>
              <th scope="col" id="table">Size</th>
              <th scope="col" id="table">S/N</th>
              <th scope="col" id="table">Model</th>
              <th scope="col" id="table">Connection Type</th>
              <th scope="col" id="table">Surplus</th>
              <th scope="col" id="table">Amount</th>
          </tr>
     </thead>
     <tbody>     
            <?php
            include 'connection.php';
            $query_misc = "SELECT * FROM all_parts WHERE part_type='MISC'";
            $result_misc = mysql_query($query_misc);
            while($row = mysql_fetch_array($result_misc))
            {
                echo "<tr>";
                echo "<td><div align=\"center\">".$row['part_type']."</div></td>";
                echo "<td><div align=\"center\">".$row['part_size']."</div></td>";
                echo "<td><div align=\"center\">".$row['part_sn']."</div></td>";
                echo "<td><div align=\"center\">".$row['part_model']."</div></td>";
                echo "<td><div align=\"center\">".$row['part_connection']."</div></td>";
                echo "<td><div align=\"center\">".$row['part_surplus']."</div></td>";
                echo "<td><div align=\"center\">".$row['part_temp_amount']."/".$row['part_amount']."</div></td>";
                echo "</tr>";
            }

..... etc

Any ideas?

EDIT:

I do now know how to put what I click on, into the url properly..

    <?
        $query = 'SELECT printer_id, printer_model FROM printer_table WHERE 1 ORDER BY name';
        $products = mysql_query($query);
        while ($product = mysql_fetch_assoc($products)) :
    {
        echo "<li><a href=\"inventory_admin.php?product_id='".$product['printer_id']."'\" onclick=\"toggleVisibility('".$product['printer_model']."')\">" .$product['printer_model']."</a></li>
";
    }

     ?>

or

    <li><a href="inventory_admin.php?product_id=<?=$product['printer_id']?>" onclick="toggleVisibility('<?=$product['printer_model']?>')"><?=$product['printer_model']?></a></li>

doesnt work properly, because i need to call the suggested part list php part, but do not know where to do so..

EDIT:

I placed that php within a div and the echos will fill out my table, but how am I suppose to call that div to only run when that onclick action? I know that is not how divs work, It would seem I would need a JS function but I know you cannot do php within JS. AJAX perhaps?

EDIT----------------------------------------------------

The call here to the DB is correct, tested it manually without any errors, but I am still receiving a syntax error upon loading my home page. Here is my call to the DB via PHP...

    <?
    if (!isset($_GET['action']))
    {
        //If not isset -> set with dumy value
        $_GET['action'] = "undefine";
    } 
    include 'connection.php';
    $query = 'SELECT ppart_table.* FROM ppart_table LEFT JOIN printer_part_relation ON ppart_table.part_id = printer_part_relation.part_id WHERE printer_part_relation.printer_id ='.mysql_real_escape_string($_GET['printer_id']);

    $parts = mysql_query($query) or die("Query failed with error: ".mysql_error());
    while ($row = mysql_fetch_assoc($parts)) 
    {
        echo table blah blah    
    }       
?>

I have tried supressing all errors via this:

    <?php error_reporting (E_ALL ^ E_NOTICE); ?>

And the syntax error at page load still exist.

My home page of this site does NOT have the "product_id=" after mysite.php

  • 写回答

1条回答 默认 最新

  • dongqu3623 2011-07-28 23:40
    关注

    I would recommend using PHP's PDO class for database access. I would also recommend using JQuery for all of you javascripting. It makes things VERY simple. It would also be advantagious to keep your display code away from your business logic (two languages should for the most part not be in the same source file ie: HTML/PHP/Javascript). I will stick with what is familiar for now however. perhaps something like this?

    menu

    <ul>
    
       <?
          $query = 'SELECT id, name FROM product_category_table WHERE 1 ORDER BY name';
          $product_categories = mysql_query($query);
          while ($product_category = mysql_fetch_assoc($product_categories)) :
       ?>
    
       <li>
          <a href="#" onclick="toggleVisibility('<?= $product_category['id'] ?>')">$product_category['name']</a>
          <ul id="<?= $product_category['id'] ?>" style="display: none;">
    
             <?
                $query = 'SELECT id, name FROM product_table WHERE category_id = ' . $product_category['id'] . ' ORDER BY name';
                $products = mysql_query($query);
                while ($product = mysql_fetch_assoc($products)) :
             ?>
    
             <li><a href="thispage.php?product_id=<?= $product['id'] ?>"><?= $product['name'] ?></a></li>
    
             <?
                endwhile;
             ?>
    
          </ul>
       </li>
    
       <?
          endwhile;
       ?>
    
    </ul>
    

    javascript

    function toggleVisibility(category) {
       var element = document.getElementById(category);
       if (element.style.display == 'none') {
          element.style.display = 'block';
       } else {
          element.style.display = 'none';
       }
    }
    

    part list

    <?
    $query = 'SELECT part_table.* FROM part_table LEFT JOIN product_to_part_associations_table ON parts_table.id = product_to_part_associations_table.part_id WHERE product_to_part_associations_table.product_id = ' . mysql_real_escape_string($_GET['product_id']);
    $parts = mysql_query($query);
    while ($part = mysql_fetch_assoc($parts)) {
       echo part table blah blah
    }
    ?>
    

    db tables

    product_category_table
    id, name, etc
    
    product_table
    id, category_id, name, etc
    
    part_table
    id, name, etc
    
    part_to_product_association_table
    id, product_id, part_id
    

    set the proper indexes on your table columns for speed. the part_to_product_association table allows for a many to many relationship between records in the product table and records in the parts table (needed in the case one part could be used in multiple products, and one product can use multiple parts).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)