duanlu1950 2012-01-06 12:41
浏览 29
已采纳

PHP - 如果声明,需要更好的解决方案来完成这个简单的任务

I am creating a directory listing, and am struggling with directing a php if statement to trigger on the right event.

The statement controls the displaying of two paragraphs.

It looks like this:

if ($userIsLoggedIn && $_SESSION['accountNum'] == $lstId)
{   
    $pageContent .= "
    <span class='editListing'><a href='/listings/edit/basic/index.php'>Edit Listing...</a></span>
";
} else {
$pageContent .= "
    <span class='editListing'>Your Listing? <a href='/listings/edit/basic/index.php'>Login to edit...</a></span>
";
}

$userIsLoggedIn is a function that checks to see if a user had logged into the website. (When a user logs into the website, their account number is then stored as a session variable.)

The variable $lstId can be explained like this:

while ($row = mysql_fetch_array($data)) {
$lstId = $row['id'];
...

Where is it the value of the field id of any specific row that is stored within a mysql table.

Now, say a user searchs a string that returns 2 results. Each result is held within a container, and the loop causes the container to repeat, with each row result that is found as a match.

Inside of the container, is where the if statement comes into effect.

What i basically want to do, is for each listing, the user is prompted with "Your listing? Log in to edit" And if the user selects this link, they are directed to the login page, once logged in they are able to edit the listing that correlates to their user account.

But the problem i am having, is that $lstId seems to be any id of any row that is returned, so that basically, when a user logs into the website, the paragraph "edit listing" is displayed in each and every contained result, even the ones that do not relate to their account. If the user clicks this link, they are directed to a page where they can edit their own listing.

The effect i am aiming for, is that the paragraph "edit listing" will only appear in the result that contains their own listing. And in every other listing the paragraph will remain as "your listing, log in to edit".

Here is the html in full that is used to generate each result:

if ($tempVar == 0) {
    $pageContent .= "
    <div class='searchResultS'>
    ";
    if ($total_pgs > 1)
    {
        $pageContent .= "
        <p>$paginator</p>
        ";
    }
}

$pageContent .= "
        <div class='searchResult'>
            <div class='searchResultWrapper'>
";

                if ($lstPage !== "")
                {   
                    $pageContent .= "
                        <span class='moreDetails'><a href='/listings/$lstPage.php'>More Details...</a></span>
                    ";
                }

$pageContent .= "
                    <h3>$lstName</h3>
 ";

                if ($userIsLoggedIn && $_SESSION['accountNum'] == $lstId)
                {   
                    $pageContent .= "
                        <span class='editListing'><a href='/listings/edit/basic/index.php'>Edit Listing...</a></span>
                    ";
                } else {
                    $pageContent .= "
                        <span class='editListing'>Your Listing? <a href='/listings/edit/basic/index.php'>Login to edit...</a></span>
                    ";
                }

$pageContent .= "
                <br style='clear:both; height:0; font-size: 1px; line-height: 0px;' />
                <div class='bizDesc'>
                    <p><span class='listHeadings'>Description:</span>  $lstDesc</p>
                </div>
                <div class='bizAddr'>
                    <p><span class='listHeadings'>Street:</span>  $lstAddress</p>
                    <p><span class='listHeadings'>Town:</span>  $lstArea</p>
                    <p><span class='listHeadings'>Province:</span>  $lstProvince</p>
                    <p><span class='listHeadings'>Post Code:</span>  $lstPostcode</p>
                    <p><span class='listHeadings'>GPS Co-Ords:</span>  $lstGps</p>
                </div>
                <div class='bizCont'>
                    <p><span class='listHeadings'>Contact:</span>  $lstContact</p>
                    <p><span class='listHeadings'>Tel:</span>  $lstTel</p>
                    <p><span class='listHeadings'>Cell:</span>  $lstCell</p>
                    <p><span class='listHeadings'>Fax:</span>  $lstFax</p>
                    <p><span class='listHeadings'>Skype:</span>  $lstSkype</p>
                    <p><span class='listHeadings'>eMail:</span>  <a href='mailto: $lstEmail?subject=Your listing on bergbiz.co.za'>$lstEmail</a></p>
                </div>
            </div>
        </div><!-- end .searchResult -->
";
$tempVar ++;
}

If anyone has any input, suggestions or insight regarding this, it would be greatly appreciated if you could share some of it with me!!

Thank you!

EDIT:

Code placed directly after the if statement within the loop:

$td = $_SESSION['accountNum'];
echo"session var is $td";
echo"lst id is $lstId";

Output:

session var is 10005lst id is 10003session var is 10005lst id is 10004session var is 10005lst id is 10002session var is 10005lst id is 10005

Any ideas why it is coming out like this? the session var stays the same, but the id variable is being looped?

  • 写回答

2条回答 默认 最新

  • dtrn74832 2012-01-06 12:45
    关注

    The first thing I'd note is that your if statement is using = and not ==, so it would return true for any $lstId. See if changing this:

    $_SESSION['accountNum'] = $lstId
    

    to this:

    $_SESSION['accountNum'] == $lstId
    

    doesn't fix the immediate issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路