我正在尝试使用数组的键作为值来更新数据库中的列。有问题的列是行的排序顺序。
让我们说每行的排序列值是1,3,5,8,9,10,12。
执行代码时,将更新为读取1,2,3,4,5,6,7。
下面我的代码执行了当我回显值时我想要实现的内容但是我遇到了更新查询的问题。
<Page
x:Class="WindowsPhone_Crawler.Views.CategoryPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WindowsPhone_Crawler.Views"
xmlns:controls="using:WindowsPhone_Crawler.Controls"
xmlns:converters="using:WindowsPhone_Crawler.Converter"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid x:Name="LayoutRoot" Background="#ebebeb">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Pivot Grid.Row="1"
Margin="-20,-35,-20,0"
x:Name="MyPvNews"
SelectionChanged="MyPvNews_SelectionChanged">
<PivotItem Background="#ebebeb" x:Name="PvHotNews">
<Grid Grid.Row="1" Background="#ebebeb">
<ScrollViewer Name="OuterScroll" Loaded="OuterScroll_Loaded" ViewChanged="OuterScroll_ViewChanged">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="#ebebeb">
<Image Source="/Assets/down13.png" Height="40" Width="40" HorizontalAlignment="Left" Margin="20,0,0,0">
<Image.RenderTransform>
<RotateTransform x:Name="RefreshIndicatiorRotateTransform" CenterX="20" CenterY="20"/>
</Image.RenderTransform>
</Image>
<TextBlock Name="RefreshIndicatiorTextBlock" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="80,0,0,0" Width="200" Foreground="Black"/>
</Grid>
<ListView x:Name="listViewHotNews" Grid.Row="1" IsItemClickEnabled="True" SelectionChanged="listViewHotNews_SelectionChanged" ItemClick="listViewHotNews_ItemClick">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="LightGray" BorderThickness="0,0,0,1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.4*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Grid.RowSpan="2" Grid.Column="0" Source="{Binding Image, Converter={StaticResource StringToImageConverter}}"/>
<TextBlock Text="{Binding Title}" MaxLines="3" TextTrimming="WordEllipsis" Foreground="Black" TextWrapping="Wrap" Grid.Row="0" Grid.Column="1" FontSize="20" FontWeight="Medium" Margin="15,12,0,0"/>
<StackPanel Orientation="Horizontal" Margin="15,0,0,0" Grid.Row="1" Grid.Column="1">
<TextBlock Foreground="Blue" Text="{Binding Name}" TextWrapping="Wrap" Grid.Row="1" Grid.Column="1" FontSize="15" FontWeight="Medium"/>
<TextBlock Foreground="Gray" Text="{Binding Modified_date, Converter={StaticResource TimesConverter}}" TextWrapping="Wrap" Grid.Row="1" Grid.Column="2" FontSize="15" FontWeight="Medium" Margin="5,0,0,0"/>
</StackPanel>
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<TextBlock x:Name="TbNoHotNews" VerticalAlignment="Center" Grid.Row="1" FontSize="22" FontWeight="Bold" Foreground="Black" HorizontalAlignment="Center" Visibility="Collapsed">Không có tin nào !</TextBlock>
</Grid>
</ScrollViewer>
</Grid>
</PivotItem>
<PivotItem Background="#ebebeb" x:Name="PvNews">
<Grid Grid.Row="1" Background="#ebebeb">
<GridView x:Name="gridViewTopic" Grid.Row="1"
SizeChanged="gridViewTopic_SizeChanged"
SelectionChanged="gridViewTopic_SelectionChanged"
ItemClick="gridViewTopic_ItemClick"
IsItemClickEnabled="True"
ScrollViewer.VerticalScrollMode="Enabled"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
</Style>
</GridView.ItemContainerStyle>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid MaximumRowsOrColumns="2" Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate>
<Grid Visibility="{Binding Regis, Converter={StaticResource VisibilityConverter}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"/>
</Grid.ColumnDefinitions>
<Grid Margin="10" >
<Grid.Background>
<ImageBrush ImageSource="{Binding IconLink, Converter={StaticResource StringToImageConverter}}" />
</Grid.Background>
<TextBlock Foreground="White" Text="{Binding TopicName}" FontSize="23"></TextBlock>
</Grid>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
<TextBlock x:Name="TbNoNews" VerticalAlignment="Center" Grid.Row="1" FontSize="22" FontWeight="Bold" Foreground="Black" HorizontalAlignment="Center" Visibility="Collapsed">Không có tin nào !</TextBlock>
</Grid>
</PivotItem>
</Pivot>
<ProgressRing x:Name="myLoading" Grid.Row="1" Background="White" Foreground="Blue" IsActive="True"></ProgressRing>
<Grid Background="#008FE5" Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<ToggleButton Margin="10,20,-30,-10" Background="Transparent" VerticalAlignment="Center" IsChecked="{Binding IsLeftPaneOpen, ElementName=HamburgerMenu, Mode=TwoWay}" Style="{StaticResource HamburgerToggleButton}" />
</Grid>
<Grid Grid.Column="1" Margin="0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Background="Transparent" x:Name="GHotNews" Tapped="GHotNews_Tapped" >
<TextBlock x:Name="TbHotNews" Text="Tin hot" FontSize="22" Foreground="White" FontWeight="Bold" VerticalAlignment="Center"/>
<Line x:Name="LHotNews" X1="0" X2="80" Stroke="#FF8B3D" StrokeThickness="2" VerticalAlignment="Bottom">
</Line>
</Grid>
<Grid Grid.Column="1" Background="Transparent" Margin="30,0,-50,0" x:Name="GNews" Tapped="GNews_Tapped" >
<TextBlock x:Name="TbNews" Text="Chuyên mục" FontSize="22" Foreground="White" FontWeight="Bold" Opacity="0.6" VerticalAlignment="Center"/>
<Line x:Name="LNews" X1="0" X2="125" Stroke="#FF8B3D" StrokeThickness="2" VerticalAlignment="Bottom" Visibility="Collapsed"/>
</Grid>
</Grid>
</Grid>
</Grid>
</Grid>
</Grid>
</Page>
使用以下内容仅更新第一行:
$sql = "SELECT asset, sort FROM portfolio ORDER BY sort ASC";
$query = mysqli_query($conn, $sql);
$i = 0;
while ($row = mysqli_fetch_assoc($query)){
$asset[$i] = $row['asset'];
$sort[$i] = $row['sort'];
$new_sort = $i+1;
$assets = implode(',', $asset);
$update_sql = "UPDATE portfolio SET sort='$new_sort' WHERE asset IN ('$assets')";
mysqli_query($conn, $update_sql);
$i++;
}
正在使用以下所有行进行更新,但是它们都被设置为最后一个值,在本例中它将是7。
$assets = implode(',', $asset);
更新:$ assets是一个字符串,例如google,yahoo,apple等。
我出错的任何想法? 非常感谢。
答案 0 :(得分:1)
这是您所需输出的解决方案。
$sql = "SELECT asset, sort FROM portfolio ORDER BY sort ASC";
$query = mysqli_query($conn, $sql);
$i = 0;
while ($row = mysqli_fetch_assoc($query)){
$asset[$i] = $row['asset'];
$sort[$i] = $row['sort'];
$new_sort = $i+1;
$assets = implode(',', $asset);
$update_sql = "UPDATE portfolio SET sort='$new_sort' WHERE asset like '".$row['asset']."'";
mysqli_query($conn, $update_sql);
$i++;
}
答案 1 :(得分:1)
每次循环时,您都会为每个资产设置new_sort
。
第一循环:
$assets[0] => "asset1"
$new_sort => 2
Update new_sort = 2 where asset in $assets. (So only asset1 will be updated.)
第二次循环:
$assets[0] => "asset1"
[1] => "asset2"
$new_sort => 3
Update new_sort = 3 where asset in $assets (So asset1 and asset 2 will be updated)
这将导致始终将每个资产的new_sort设置为最新的new_sort值;
输出的代码可能是:
while ($row = mysqli_fetch_assoc($query)){
$new_sort = $i+1;
$update_sql = "UPDATE portfolio SET sort='" . $new_sort . "' WHERE asset='" . $row['asset'] . "';";
mysqli_query($conn, $update_sql);
$i++;
}
答案 2 :(得分:0)
首先,您需要在while循环之外移动UPDATE查询,而不是像这样使用:
$assets = implode("','", $asset); // split $asset in comma and single quote.
$update_sql = "UPDATE portfolio SET sort='$new_sort' WHERE asset IN ('$assets')";
您已将值存储在数组$asset[$i] = $row['asset'];
中,因此无需在while循环中使用查询。
在您的代码中,您将更新$i
列中sort
的最后一个值。
另一个解决方案非常简单,将单个记录更新为:
<?php
$sql = "SELECT asset, sort FROM portfolio ORDER BY sort ASC";
$query = mysqli_query($conn, $sql);
$i = 0;
while ($row = mysqli_fetch_assoc($query)){
$new_sort = $i+1;
$assets = $row['asset'];
$update_sql = "UPDATE portfolio SET sort = '$new_sort' WHERE asset = '$assets'";
mysqli_query($conn, $update_sql);
$i++;
}
?>