我正在研究从mySQL数据库中提取数据的方法,并将文本显示为数组但是我有这样的方式,因此只有一个数据字符串可用,每个实例都用逗号示例分隔。 1,2,3,4有没有办法让它在每个逗号之后断开或开始一个新行,而不是像这样的集合混乱:
1 package KRAFT Deluxe Macaroni & Cheese Dinner Sharp Cheddar Cheese Sauce, 1 lb Ground Beef, 1 Jar Spaghetti Sauce, 1/2 Cup KRAFT Shredded Mozzarella Cheese
代码:
<?php
require_once '../scripts/app_config.php';
require_once '../../scripts/authorize.php';
require_once '../scripts/database_connection.php';
// Get the user ID of the user to show
$User_id = $_REQUEST['User_id'];
// Gets User Id
$select_query = "SELECT * FROM users WHERE User_id = " . $User_id;
// Run the query to get User Name
$result1 = mysql_query($select_query);
if ($result1) {
$row = mysql_fetch_array($result1);
$first_name = $row['First_name'];
$last_name = $row['Last_name'];
$email = $row['Email'];
$User_id = $row['User_id'];
} else {
handle_error("There was a problem finding your " .
"information in our system.",
"Error locating user with ID {$User_id}");
}
$Recipe_Id = $_REQUEST['Id'];
$result = mysql_query( "SELECT * FROM recipes WHERE Id = " . $Recipe_Id )
or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$Id = $row['Id'];
$Name = $row['Name'];
$Ingredients = $row['Ingredients'];
$Category1 = $row['Category1'];
$Category2 = $row['Category2'];
$Directions = $row['Directions'];
$Creator = $row['User_Name'];
?>
<html>
<head>
<title>Recipes</title>
<meta name="viewport" content="width=device-width">
<meta http-equiv="Content-Language" content="English">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="../style/index.css" media="screen">
<script type="text/javascript" src="jquery-1.9.1.min.js">
</script>
</head>
<body>
<div id="header">
</div>
<center>
<span class="nav"><a href="recipes/index.php">Home</a></span> <span class="nav"><a href="../recipes/addrecipe/index.php">New Recipe</a> </span> <span class="nav"><a href="../scripts/signout.php">Sign Out</a> </span>
</center>
<br>
<div id="content">
<div class="user_profile">
<h1><?php echo "{$first_name} {$last_name}";?></h1>
</div>
<div id="feature">
<?php
echo "<H1>". $row['Name'] ."</H1>";
echo "<p>" . $row['Category1'] . "</p>";
echo "<p>" . $row['Category2'] . "</p>";
echo "<p>" . $row['Creator'] . "</p>";
?> </div>
<div class="column1">
<h1>My Recipes</h1>
<?php
echo "<p>" . $row['Ingredients'] . "</p>";
?>
</div>
<div class="column2">
<h1>Directions</h1>
<?php
echo "<p>" . $row['Directions'] . "</p>";
}
?>
</div>
<div class="column3">
<h1>Recipes I Like</h1>
</div>
</div>
<div id="footer"></div>
</body>
</html>
答案 0 :(得分:0)
如果我说得对,你只需要这样的东西:
echo "<p>" . str_replace(', ',', <br />',$row['Ingredients']) . "</p>";