将PHP循环转换为C#
问题描述:
我试图将PHP中的页面转换为C#。 PHP的页面是这样的:将PHP循环转换为C#
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sectors</title>
<link rel="stylesheet" href="style/default.css">
</head>
<body class="trombinoscope_body">
<?php
//MySQL Database Connect
include 'connect.php';
$var_sector = $_GET['varsector'];
$sql = "SELECT * FROM employees";
$result = mysqli_query($connect,$sql);
if($result) {
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
?>
<div class="sector_title">
<?php echo $var_sector; ?>
</div>
<?php
$level = null;
foreach($rows as $row) {
if ($level != $row['level']) {
if (isset ($level)) {
echo '<br/>';
}
$level = $row['level'];
}
?>
<div class="people_box">
<img class="people_photo" src="photos/<?php echo $row['photo']; ?>">
<div class="name_text">
<p><?php echo $row['name']; ?></br></p>
</div>
<div class="function_text">
<p style="margin-top: -5px"><?php echo $row['function']; ?></br></p>
</div>
<div class="info_box">
<img class="line" src="images/line.png">
<!-- Sector Information -->
<img class="sector_icon" src="images/sector.png"><p class="info_text"><?php echo $row['sector']; ?></br></p>
<!-- Location Information -->
<img class="location_icon" src="images/location.png"><p class="info_text"><?php echo $row['location']; ?></br></p>
<!-- Mail Information -->
<img class="mail_icon" src="images/mail.png"><p class="info_text_link"><a href="mailto:<?php echo $row['mail']; ?>"><?php echo $row['mail']; ?></a></br></p>
<!-- Phone Information -->
<img class="phone_icon" src="images/phone.png"><p class="info_text"><?php echo $row['phone']; ?></br></p>
<!-- Skype Information -->
<img class="skype_icon" src="images/skype.png"><p class="info_text_link"><a href="sip:<?php echo $row['mail']; ?>">Skype for Business</a></br></p>
</div>
</div>
<?php } } ?>
</body>
</html>
我已经这样做(我不知道这是否是完全正确的,不是真正的PHP页面我知道,但仅此而已)
Default.aspx页
<%@ Page
Title="Home Page"
Language="C#"
MasterPageFile="~/Site.Master"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="trombinoscope._Default"
%>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:Repeater ID="people_box_repeater" runat="server">
<ItemTemplate>
<div class="people_box">
<img class="people_photo" src="photos/<%#((System.Data.DataRowView)Container.DataItem)["photo"] %>">
<!-- Name Information -->
<div class="name_text">
<p><%#((System.Data.DataRowView)Container.DataItem)["name"] %></br></p>
</div>
<!-- Function Information -->
<div class="function_text">
<p style="margin-top: -5px"><%#((System.Data.DataRowView)Container.DataItem)["function"] %></br></p>
</div>
<div class="info_box">
<img class="line" src="images/line.png">
<!-- Sector Information -->
<img class="sector_icon" src="images/sector.png"><p class="info_text"><%#((System.Data.DataRowView)Container.DataItem)["sector"] %></br></p>
<!-- Location Information -->
<img class="location_icon" src="images/location.png"><p class="info_text"><%#((System.Data.DataRowView)Container.DataItem)["location"] %></br></p>
<!-- Mail Information -->
<img class="mail_icon" src="images/mail.png"><p class="info_text_link"><a href="mailto:<%#((System.Data.DataRowView)Container.DataItem)["mail"] %>"><%#((System.Data.DataRowView)Container.DataItem)["mail"] %></a></br></p>
<!-- Phone Information -->
<img class="phone_icon" src="images/phone.png"><p class="info_text"><%#((System.Data.DataRowView)Container.DataItem)["phone"] %></br></p>
<!-- Skype Information -->
<img class="skype_icon" src="images/skype.png"><p class="info_text_link"><a href="sip:<%#((System.Data.DataRowView)Container.DataItem)["mail"] %>">Skype for Business</a></br></p>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</asp:Content>
Default.aspx.cs页面
using System;
using System.Configuration;
using System.Data;
using MySql.Data.MySqlClient;
namespace trombinoscope
{
public partial class _Default : System.Web.UI.Page
{
public void Page_Load(object sender, EventArgs e) => People_box_repeater_method();
public void People_box_repeater_method()
{
using (MySqlConnection mysql_connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
MySqlDataAdapter mysql_data_adapter = new MySqlDataAdapter("SELECT * FROM employees", mysql_connection);
DataTable data_table = new DataTable();
mysql_data_adapter.Fill(data_table);
DataView data_view = new DataView(data_table);
people_box_repeater.DataSource = data_view;
people_box_repeater.DataBind();
}
}
}
}
但我不能在特定的转换这部分:
<?php
$level = null;
foreach($rows as $row) {
if ($level != $row['nivel']) {
if (isset ($level)) {
echo '<br/>';
}
$level = $row['nivel'];
}
?>
基本上,这部分代码需要从数据库中的行中的“文化程度”一栏,并检查它们是否相同,如果是同样,代码不做任何事情,如果它不同,则代码写入</ br>
,导致它破坏一条线。
没有我做的事情接近我可以改善的结果。我只需要一个方向。
答
我设法使它像这样的工作,我就把这两条线在模板中:
<asp:Label ID="jump_line_label" runat="server"></asp:Label>
<asp:Label ID="retrieve_data_from_aspx" style="display: none;" runat="server" Text='<%#((System.Data.DataRowView)Container.DataItem)["level"] %></asp:Label>
而且在后面的代码我这样做:
public void People_box_repeater_method()
{
mysql_string = "SELECT * FROM employees ORDER BY level ASC";
mysql_data_adapter = new MySqlDataAdapter(mysql_string, mysql_connection);
DataTable data_table = new DataTable();
mysql_data_adapter.Fill(data_table);
DataView data_view = new DataView(data_table);
people_box_repeater.DataSource = data_view;
people_box_repeater.DataBind();
string jump_line = null;
foreach (RepeaterItem item in people_box_repeater.Items)
{
var retrieve_data_from_aspx = ((Label)item.FindControl("retrieve_data_from_aspx")).Text;
var jump_line_label = ((Label)item.FindControl("jump_line_label"));
if (retrieve_data_from_aspx != jump_line)
{
jump_line_label.Text = "</br>";
}
jump_line = retrieve_data_from_aspx;
}
}
和它的工作!
https://msdn.microsoft.com/en-us/library/bb348436%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396(需要了解https://docs.microsoft.com/EN-US/DOTNET/CSHARP /编程引导/报表表达式运营商/平等,比较) – decPL