如果是php语法重定向中的else语句

时间:2013-10-10 09:40:14

标签: php arrays session

您好我正在研究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);
?>

8 个答案:

答案 0 :(得分: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);
?>

答案 1 :(得分:1)

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

开始
$output = '';

然后替换

$output[] =

$output .=

然后您可以echo $output;代替echo join('',$output);来输出值。

答案 2 :(得分: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 **;**
}

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

答案 3 :(得分: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>';
};

答案 4 :(得分: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;
?>

答案 5 :(得分: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>
};

答案 6 :(得分: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);
?>

答案 7 :(得分: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>";
?>