1

您好,我正在开发 PHP 在线购物项目。我设计了一个购物车,条件是如果用户登录,他可以在他的购物车中添加项目,否则他将被重定向到登录页面。但是当我使用数组加入我的语法时,我无法获得所需的输出.我的代码如下:

<?php
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');
// Include functions
require_once('inc/functions.inc.php');
session_start();
$sql = 'SELECT * FROM products ORDER BY id';
$result = $db->query($sql);
$output[] = '<ul>';
while ($row = $result->fetch()) {
$output[] = '<li>"'.$row['title'].'" by </br> '.$row['author'].': &pound;'.$row['price'].'<img src="http://localhost/myproject/images/'.$row['image'].'" width="100" height="100" /><br />
.if (isset($_SESSION['username'])) {. 
<a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>'
.}
else {.
'<a href="login.php">Add to cart</a></li>'
};
//$output[] = '</ul>';
echo join('',$output);
?>
4

8 回答 8

2

尝试

<?php
session_start();
$sql = 'SELECT * FROM products ORDER BY id';
$result = $db->query($sql);

$output[] = '<ul>';

while ($row = $result->fetch())
{
    if (isset($_SESSION['username']))
    { 
        $url = 'cart.php?action=add&id='.$row['id'];
    }
    else {
        $url = 'login.php';
    }

    $output[] = '<li>"'.
        $row['title'].'" by </br>'.
        $row['author'].
        ': &pound;'.
        $row['price'].
        '<img src="http://localhost/myproject/images/'.$row['image'].
        '" width="100" height="100" /><br /><a href="'.$url.'">Add to cart</a></li>';
    $output[] = '</ul>';
}
echo join('',$output);
?>
于 2013-10-10T09:44:16.997 回答
1

您不需要创建输出数组,只需创建一个字符串并添加到其中。

从...开始

$output = '';

然后替换

$output[] =

$output .=

然后你可以做echo $output;而不是echo join('',$output);输出值。

于 2013-10-10T09:43:23.513 回答
1

字符串文字末尾缺少 ' 的经典问题以及三元运算符的使用:

$output[] = '<li>"'.$row['title'].'" by </br> '.$row['author'].': &pound;'.$row['price'].'<img src="http://localhost/myproject/images/'.$row['image'].'" width="100" height="100" /><br />'
.  (isset($_SESSION['username']) ? 
      '<a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>'
      : '<a href="login.php">Add to cart</a></li>'); // don't forget **;**
}

如果您不需要将其放在数组中,最好将其与@GarethLucett 建议的解决方案配对:只需将字符串连接在一起,无需加入数组。

于 2013-10-10T09:44:04.547 回答
1

试试这个方法:

$output = '<li>"'.$row['title'].'" by </br> '.$row['author'].': &pound;'.$row['price'].'<img src="http://localhost/myproject/images/'.$row['image'].'" width="100" height="100" /><br />';
if (isset($_SESSION['username'])) {
$output.= '<a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
}
else {
$output.='<a href="login.php">Add to cart</a></li>';
};
于 2013-10-10T09:44:17.650 回答
1

你的代码应该如下:

<?php
session_start();
$sql = 'SELECT * FROM products ORDER BY id';
$result = $db->query($sql);
$output = '<ul>';
while ($row = $result->fetch()) {
    $output .= '<li>"'.$row['title'].'" by </br> '.$row['author'].': &pound;'.$row['price'].'<img src="http://localhost/myproject/images/'.$row['image'].'" width="100" height="100" /><br />';
    if (isset($_SESSION['username'])) { 
        $output .= '<a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
    }
    else {
        $output .= '<a href="login.php">Add to cart</a></li>';
    }
}
$output .= '</ul>';
echo $output;
?>
于 2013-10-10T09:44:31.950 回答
1
while ($row = $result->fetch()) {
   $link = (isset($_SESSION['username'])) ? "<a href="cart.php?action=add&id={$row['id']}">Add to cart</a>" : "<a href='login.php'>Add to cart</a>";
   $output[] = "<li> '{$row['title']}' by </br>{$row['author']}: &pound; {$row['price']} <img src='http://localhost/myproject/images/{$row['image']}' width='100' height='100' /><br /> {$link} </li>
};
于 2013-10-10T09:48:10.210 回答
1

看来,您还没有关闭 while 循环。这样做:

<?php
session_start();
$sql = 'SELECT * FROM products ORDER BY id';
$result = $db->query($sql);
$output[] = '<ul>';
while ($row = $result->fetch()) {
    $str = '<li>"'.$row['title'].'" by </br> '.$row['author'].': pound;'.$row['price'].
    '<img   src=\"http://localhost/myproject/images/'.$row['image'].'\" width="100" 
    height="100" /><br />';
    if (isset($_SESSION['username'])) {
        $str .= '<a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
    }
    else {
        $str .= '<a href="login.php">Add to cart</a></li>';
    };
    $output[] = $str;
}
$output[] = '</ul>';
echo join('', $output);
?>
于 2013-10-10T09:54:47.893 回答
1

尝试:

<?php
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');
// Include functions
require_once('inc/functions.inc.php');
session_start();
$sql = 'SELECT * FROM products ORDER BY id';
$result = $db->query($sql);

$output = array();

while ($row = $result->fetch()) {
    $data = '<li>'.$row['title'].' by </br> '.$row['author'].': &pound;'.$row['price'].'<img src="http://localhost/myproject/images/'.$row['image'].'" width="100" height="100" /><br />';
    if (isset($_SESSION['username'])) {
        $data .= '<a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
    } else {
        $data .='<a href="login.php">Add to cart</a></li>';
    }
    $output[] = $data;
}
//$output[] = '</ul>';
$output_string =  join('',$output);
echo "<ul>".$output_string."</ul>";
?>
于 2013-10-10T09:55:01.827 回答