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

时间:2015-08-04 00:30:30

标签: php mysql

我正在尝试建立一个乘法系统,以便为消费者提供我认为他们想要的产品结果。我是PHP和MYSQL的新手,所以如果你能添加一些很棒的解释!如果没有,那么我会弄明白的!

因此每个产品的评分均为1-3。
___________________________________________
名称|柔软度|温暖
第1项| 2 | 1
第2项| 3 | 3
第3项| ...... | ...
...... | ...... | ...

在产品页面上,它会询问消费者他们在寻找什么。

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

我希望下拉菜单值与mysql数据库中的列值相乘。

顾客选择柔软度:1和温暖:2 项目1:2,2 第2:3,6 第3项:等 继续使用所有产品

然后它将显示具有最高值的前3项。

问题页面:

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();

    ?>

2 个答案:

答案 0 :(得分:0)

如果是帖子,并且您使用的是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());

}

然后你可以回显出数组中的值。 基本上,您正在访问项目表中的所有信息,然后您将在while循环中逐个浏览它。对于每一行,您要将数据库中的值乘以发布值,然后将该值放入数组中。

然后,您可以使用

访问该数组
echo $weight_values[0][1]; //for item with id 1 softness value
echo $weight_values[0][2]; //for item with id 1 warmth value    

如果您使用的是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));

} 

答案 1 :(得分:0)

考虑到纯粹的Mysql实现,

  1. 所有输入都经过消毒处理。逃脱了。
  2. 所有其他编程原则/因素/良好实践均得到妥善处理。
  3. 恕我直言如下,

    <Style TargetType="PivotHeaderItem">
        <Setter Property="FontSize" Value="{ThemeResource PivotHeaderItemFontSize}" />
        <Setter Property="FontFamily" Value="{ThemeResource PivotHeaderItemFontFamily}" />
        <Setter Property="FontWeight" Value="{ThemeResource PivotHeaderItemThemeFontWeight}" />
        <Setter Property="CharacterSpacing" Value="{ThemeResource PivotHeaderItemCharacterSpacing}" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Foreground" Value="Red" /> <!-- original value {ThemeResource SystemControlForegroundBaseMediumBrush} -->
        <Setter Property="Padding" Value="{ThemeResource PivotHeaderItemMargin}" />
        <Setter Property="Height" Value="48" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="PivotHeaderItem">
                    <Grid x:Name="Grid" Background="{TemplateBinding Background}">
                        <Grid.Resources>
                            <Style x:Key="BaseContentPresenterStyle" TargetType="ContentPresenter">
                                <Setter Property="FontFamily" Value="Segoe UI" />
                                <Setter Property="FontWeight" Value="SemiBold" />
                                <Setter Property="FontSize" Value="15" />
                                <Setter Property="TextWrapping" Value="Wrap" />
                                <Setter Property="LineStackingStrategy" Value="MaxHeight" />
                                <Setter Property="TextLineBounds" Value="Full" />
                                <Setter Property="OpticalMarginAlignment" Value="TrimSideBearings" />
                            </Style>
                            <Style x:Key="BodyContentPresenterStyle" TargetType="ContentPresenter" BasedOn="{StaticResource BaseContentPresenterStyle}">
                                <Setter Property="FontFamily" Value="{ThemeResource PivotHeaderItemFontFamily}" />
                                <Setter Property="FontWeight" Value="{ThemeResource PivotHeaderItemThemeFontWeight}" />
                                <Setter Property="FontSize" Value="{ThemeResource PivotHeaderItemFontSize}" />
                            </Style>
                        </Grid.Resources>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition From="Unselected" To="UnselectedLocked" GeneratedDuration="0:0:0.33" />
                                    <VisualTransition From="UnselectedLocked" To="Unselected" GeneratedDuration="0:0:0.33" />
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unselected" />
                                <VisualState x:Name="UnselectedLocked">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="ContentPresenterTranslateTransform" Storyboard.TargetProperty="X" Duration="0" To="{ThemeResource PivotHeaderItemLockedTranslation}" />
                                        <DoubleAnimation Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="(UIElement.Opacity)" Duration="0" To="0" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Green" /> <!-- original value {ThemeResource SystemControlHighlightAltBaseHighBrush} -->
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="UnselectedPointerOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="SelectedPointerOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="UnselectedPressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="SelectedPressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentPresenter x:Name="ContentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Margin="{TemplateBinding Padding}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" FontWeight="{TemplateBinding FontWeight}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                            <ContentPresenter.RenderTransform>
                                <TranslateTransform x:Name="ContentPresenterTranslateTransform" />
                            </ContentPresenter.RenderTransform>
                        </ContentPresenter>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    这种方法有一些好处,

    1. promise = element.requestFullscreen()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
    2. 之间传输的数据较少
    3. 之后你不需要在php中对它进行后处理,因为它已经为你提供了最好的三种选择。
    4. 如果表格很大而没有行,那么这个解决方案更好,因为将整个表格导入MySQL并排序/过滤前三个可能会产生内存限制错误
    5. 更新1: - 关于语法错误。

      这是一个SQL Fiddle来证明/解释我的解决方案的工作。

      惯例警告: -

      请开始使用 MySQLi 而不是 MySQL A.S.A.P.因为不这样做会使您的应用程序的安全性面临巨大风险。