简体   繁体   English

PHP / MYSQL将每行的每列乘以所选值

[英]PHP/MYSQL Multiply each column of every row by chosen value

I'm trying to make a multiplying system to give consumers results on the product that I think they would like. 我正在尝试建立一个乘法系统,以便为消费者提供我认为他们想要的产品结果。 I am pretty new at PHP and MYSQL so if you can add little explanations that would be great! 我是PHP和MYSQL的新手,所以如果你能添加一些很棒的解释! If not then I will figure it out! 如果没有,那么我会搞清楚!

So each product gets a rating from 1-3. 因此每个产品的评分均为1-3。
___________________________________________ ___________________________________________
Name | 名称| Softness | 柔软度| Warmth 热情
Item 1 | 第1项| 2 | 2 | 1 1
Item 2 | 第2项| 3 | 3 | 3 3
Item 3 | 第3项| ... | ...... | ... ...
... | ...... | ... | ...... | ... ...

On the product page it asks the consumer what they are looking for. 在产品页面上,它询问消费者他们在寻找什么。

Softness [menu 1-3] 柔软度[菜单1-3]
Warmth [menu 1-3] 温暖[菜单1-3]

I want the dropdown menu value to multiply with the column values in the mysql database. 我希望下拉菜单值与mysql数据库中的列值相乘。

Customer chooses softness: 1 and warmth: 2 Item 1 : 2, 2 Item 2: 3, 6 Item 3: etc Continues with all products 顾客选择柔软度:1和保暖:2项目1:2,2项目2:3,6项目3:等继续所有产品

Then it would display the top 3 items with the highest values. 然后它将显示具有最高值的前3项。

Question Page: 问题页面:

How important is softness in your product?
<input id="1" type="radio" name="softness" value="-1.0">
<label for="1">Not very important</label>
<input id="2" type="radio" name="softness" value="0" checked>
<label for="2">Neutral</label>
<input id="3" type="radio" name="soft" value="1.0">
<label for="3">Very important</label>

How important is warmth in your product?
<input id="1" type="radio" name="warmth " value="-1.0">
<label for="1">Not very important</label>
<input id="2" type="radio" name="warmth " value="0" checked>
<label for="2">Neutral</label>
<input id="3" type="radio" name="warmth " value="1.0">
<label for="3">Very important</label>

This is all I got so far...

    <?php

    // Connect SQL 
    include"database/sqlcon.php";

    // Select Database
    $query = mysql_query(
    "SELECT * FROM product LIMIT 3"
    );

    if(!$query) {
    die(mysql_error());
    }

    // Get Form Values
    $softness= $_POST['softness'];
    $warmth= $_POST['warmth'];

    while($row = mysql_fetch_array($query)) {
    $productname = $row['product'];
    $productsoft= $row['soft'];
    $productwarm = $row['warm'];

    }

    $totalsoft = $productsoft * $softness; 
     $totalwarm = $productwarm * $warmth;

    mysql_close();

    ?>

if it is a post, and you are using mysql PDO 如果它是一个帖子,你使用的是mysql PDO

if(isset($_POST))
{
   $items = $db->query("select * from items");
   $row_items = $items->fetch();
   $weight_values = array();
   $x= 0;

   do{
      $weight_values[$x]['1'] = $_POST['Softness']*$row_items['softness'];
      $weight_values[$x]['2'] = $_POST['Warmth']*$row_items['warmth'];
      $x++;
   }while($row_items = $items->fetch());

}

Then you can just echo out the values from the array. 然后你可以回显出数组中的值。 Basically you are accessing all the information from your items table, then you are going to go through it 1 by 1 in the while loop. 基本上,您正在访问项目表中的所有信息,然后您将在while循环中逐个浏览它。 For each row you are going to multiply the value in the database by the posted value, you are then going to put that value into an array. 对于每一行,您要将数据库中的值乘以发布值,然后将该值放入数组中。

You can then access that array by using 然后,您可以使用访问该数组

echo $weight_values[0][1]; //for item with id 1 softness value
echo $weight_values[0][2]; //for item with id 1 warmth value    

If you are using mysql which you shouldn't as given by the reason in the link 如果您使用的是mysql ,则不应该通过链接中的原因给出

if(isset($_POST))
{
   $query_items("select * from items");
   $items = mysql_query($query_items,$items_db) or die(mysql_error());
   $row_items = mysql_fetch_assoc($items);
   $weight_values = array();
   $x= 0;

   do{
      $weight_values[$x]['1'] = $_POST['Softness']*$row_items['softness'];
      $weight_values[$x]['2'] = $_POST['Warmth']*$row_items['warmth'];
      $x++;
   }while($row_items = mysql_fetch_assoc($items));

} 

A pure Mysql Implementation considering that, 一个纯粹的Mysql实现考虑到,

  1. All the inputs are sanitized & escaped. 所有输入都经过消毒和转义。
  2. All other programing principles / factors / Good Practices are taken care of. 所有其他计划原则/因素/良好实践都得到了解决。

IMHO is as follows, 恕我直言如下,

select
  *,
  softness * '. $softness .' + warmth * '. $warmth .' as totalindexy
from items2
order by totalindexy DESC
LIMIT 3 -- Then it would display the top 3 items with the highest values

and this method has a few benefits, 这种方法有一些好处,

  1. Less data travels between MySQL and php MySQLphp之间的数据传输较少
  2. You don't need to post-process it afterwards in php, since it already gives you the best three options. 之后你不需要在php中对它进行后处理,因为它已经为你提供了最好的三个选项。
  3. If the table is huge as in no of rows, then this solution is better, as importing the whole table to php and sorting/filtering the top three may produce memory limit errors. 如果表是巨大的,没有行,那么这个解决方案更好,因为将整个表导入php并排序/过滤前三个可能会产生内存限制错误。

Update 1 :- Regarding the Syntax Error. 更新1: - 关于语法错误。

Here is a SQL Fiddle to prove/explain the working of my solution. 这是一个SQL小提琴来证明/解释我的解决方案的工作。

The customary warning :- 习惯性警告: -

Please start using MySQLi instead of MySQL ASAP since not doing so shall put the security of your application at huge risk. 请尽快开始使用MySQLi而不是MySQL,因为不这样做会使应用程序的安全性面临巨大风险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM