【问题标题】:I have a business sales page that shows results from a json file. I want to be able to pass the information for one listing on a results page [duplicate]我有一个显示 json 文件结果的商业销售页面。我希望能够在结果页面上传递一个列表的信息 [重复]
【发布时间】:2021-09-20 18:00:31
【问题描述】:

我的 index.php 页面显示了来自 json 文件的列表,看起来不错。但我正在尝试将单个列表的结果从该页面发送到另一个页面(result.php)。如何将单个列表的结果显示到新页面?

这是我的第一页的代码,附加到 json 文件:

<?php
$filename = file_get_contents("/ListingCollection.json");
$listings = json_decode($filename);
?>

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
    <script src="https://kit.fontawesome.com/ac048d9955.js" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
</head>
    
<body>
<div style="margin-left:150px; margin-right:150px; margin-bottom:150px;">
<?php foreach ($listings as $listing) { ?>
 <span style="visibility:hidden;"><?= $listing->Oid; ?></span>
    <div class="row align-items-center border-top">
        <div class="col-3" style="padding:10px;">
            <?= $listing->AdPhoto; ?>
        </div>
        <div class="row col-8" style="margin-left:10px;">
            <div class="col-8 align-items-center">
                <h2 style="color:#00471C; width: 700px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;" type="button"><?= $listing->AdTitle; ?></h2>
                <h3><?= $listing->AdTagLine; ?></h3>
                <p><?= $listing->AdTagLine; ?></p>
            </div>
            <div class="col-4 align-items-start">
                <center><h1><?= $listing->ListingPrice; ?></h1><h3>EBITDA: $<?= $listing->EBITDA; ?></h3></center><br><br>
                <center><a class="btn btn-primary" href="<?= $listing->WebsiteURL; ?>?<?= $listing->Oid; ?>" role="button">See Listing</a></center>
            </div>
        </div>
    </div>
<?php  } ?>
</div>
</body>
</html>

这是我想要从 index.php 文件的单个列表中提取的第二页的代码:

<?php
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
get_header();  
$filename = file_get_contents("/ListingCollection.json");
$listings = json_decode($filename);
?>

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
    <script src="https://kit.fontawesome.com/ac048d9955.js" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
</head>

<body>
<div style="margin-left:150px; margin-right:150px; margin-top:50px; margin-bottom:50px;">
 <?php foreach ($listings as $listing) { ?>
<!-- Ad Header -->
    <div class="row align-items-center" style="margin-bottom:25px;">
        <div class="col-12">
            <h1><?= $listing->AdTitle; ?></h1>
            <h3><?= $listing->County; ?>, <?= $listing->State; ?></h3>
        </div>
    </div>
<!-- Ad Photo -->
    <div class="row" style="margin-bottom:25px;">
        <div class="row col-8 align-items-center">
            <div class="col-12">
                <?= print "Your registration is: ".$regValue."."; ?>
                <?= $listing->AdPhoto; ?><br><br>
            </div>
<!-- Ad Pricing -->
            <div class="col-6">
                <h2>ASKING PRICE: $<?= $listing->ListingPrice; ?></h2>
            </div>
            <div class="col-6">
                <h2>CASH FLOW: $<?= $listing->CashFlow; ?></h2>
            </div>
<!-- Above button info -->
                <div class="col-3">Gross Revenue: </div><div class="col-3">$<?= $listing->GrossRevenue; ?> </div><div class="col-3">Inventory: </div><div class="col-3">$<?= $listing->Inventory; ?></div>
                <div class="col-3">EBITDA: </div><div class="col-3">$<?= $listing->EBITDA; ?> </div><div class="col-3">Rent: </div><div class="col-3">$<?= $listing->Rent; ?> </div>
                <div class="col-3">FF&E: </div><div class="col-3">$<?= $listing->FFandE; ?> </div><div class="col-3">Established: </div><div class="col-3"><?= $listing->YearEstablished; ?> </div><br><br>
<!-- buttons -->
            <div class="col-3">
                <a class="btn btn-primary" style="width:100%;" type="button" href="">SAVE</a>
            </div>
            <div class="col-3">
                <a class="btn btn-primary" style="width:100%;" type="button" href="">PRINT</a>
            </div>
            <div class="col-3">
                <a class="btn btn-primary" style="width:100%;" type="button" href="">SHARE</a>
            </div>
            <div class="col-3">
                <a class="btn btn-primary" style="width:100%;" type="button" href="">VALUATION REPORT</a>
            </div>
<!-- Ad Description -->
            <div class="col-12"><br><br><hr><br>
                <h2>DESCRIPTION</h2>
                <h2><?= $listing->AdTagLine; ?></h2>
                <?= $listing->AdDescription ?><br>
                <hr><br>
            </div>
<!-- Ad Details -->
            <div class="col-3">
                <h3>Location:</h3>
            </div>
            <div class="col-9">
                <?= $listing->County ?> 
            </div>
            <div class="col-3">
                <h3>Building SF:</h3>
            </div>
            <div class="col-9">
                <?= $listing->TotalSqFt ?> 
            </div>
            <div class="col-3">
                <h3>Employees:</h3>
            </div>
            <div class="col-9">
                <?= $listing->EmployeeCount ?> 
            </div>
            <div class="col-3">
                <h3>Facilities:</h3>
            </div>
            <div class="col-9">
                <?= $listing->AdFacilityDescription ?> 
            </div>
            <div class="col-3">
                <h3>Competition:</h3>
            </div>
            <div class="col-9">
                <?= $listing->AdCompetitiveAnalysis ?> 
            </div>
            <div class="col-3">
                <h3>Growth & Expansion:</h3>
            </div>
            <div class="col-9">
                <?= $listing->AdOpportunityForGrowth ?> 
            </div>
            <div class="col-3">
                <h3>Support & Training:</h3>
            </div>
            <div class="col-9">
                <?= $listing->AdSupportAndTraining ?> 
            </div>
            <div class="col-3">
                <h3>Reason for Selling:</h3>
            </div>
            <div class="col-9">
                <?= $listing->AdReasonForSelling ?> 
            </div>
            <div class="col-3">
                <h3>Business Website:</h3>
            </div>
            <div class="col-9">
                <?= $listing->WebsiteURL ?> 
            </div>
        </div>
        <div class="col-4 bg-light">
            <div class="container">
  <!-- CHANGE THE URL HERE -->
                <div class="col-12">
                    <form action="https://app.99inbound.com/e/123" method="POST" target="_blank">
                        <h1 style="text-align: center;"><br>CONTACT US</h1>
                    <div class="form-group">
                        <input name="name" type="text" class="form-control" id="name" placeholder="Full Name" required>
                        <input name="phone" type="phone" class="form-control" id="phone" placeholder="Phone Number" required>
                        <input name="email" type="email"  class="form-control" id="email" placeholder="Enter Email" required>
                    </div>
                    <div class="form-group">
                        <textarea name="message" class="form-control" id="message" rows="5" placeholder="Enter message" required></textarea>
                    </div>
                    <button type="submit" class="btn btn-primary">Submit</button>
                    </form>
                    <br><hr>
                </div>
                <div class="row col-12">
                    <div class="col-6"><p>Business Listed By:<br>
                    <?= $listing->SellerFirstName; ?> <?= $listing->SellerLastName; ?></p></div>
                    <div class="col-6"><p><i class="fas fa-phone-square-alt"></i> <?= $listing->SellerPhone; ?></div>
                </div>
            </div>
        </div>
    </div>
<?php  } ?>
</div>
<?php get_footer(); ?>
</body>
</html>

【问题讨论】:

标签: javascript php jquery json wordpress


【解决方案1】:

我可能会对链接进行细微的更改,将?id 转换为键/值对,例如

<center><a class="btn btn-primary" href="<?= $listing->WebsiteURL; ?>?display=<?= $listing->Oid; ?>" role="button">See Listing</a></center>

然后在第二页

<?php
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
get_header();  
$filename = file_get_contents("/ListingCollection.json");
$display = $_GET['display'];
$listings = json_decode($filename);
$listing=array_filter($listings, function($a) use($display) {
    return $a->Oid === $display;
 });
$listing = array_values($listing)[0];


// get rid of the loop on this page
//  foreach ($listings as $listing) {  <-- GONE

?>

<!-- then the rest of your code, using $listing -->

【讨论】:

  • 代码没有改变 result.php 页面的结果。无论我单击 index.php 页面上的哪个按钮,它总是向我显示第一个对象。该链接现在显示“result.php?display=”加上属性的 Oid。 Oid 不是对象的 ID。我需要在我的 result.php 页面中添加什么代码来显示 index.php 页面的显示?
  • Oid 不是唯一的吗?有id 属性吗?我们可以使用它。
  • 确保您没有在列表详情页面上使用该循环&lt;?php foreach ($listings as $listing) { ?&gt;。我在回答中的代码应该给你$listing 变量
  • Oid 是独一无二的。我对那个评论不好。只是确认一下,当您提到“列表页面”时,您是在谈论 index.php 还是 result.php 页面?
  • 是的,只说result.php页面。在结果页面上,应该有一个用于 oid 的 GET 变量,我称之为 $display。 array_filter 代码应该清除除您想要的项目之外的所有项目,但它将作为一个包含一个项目的数组出现。因此,行 $listing = $listing[0];请注意,我更新了我的答案以表明您希望摆脱结果页面上的外部循环。
【解决方案2】:

我通过对按钮执行此操作解决了问题:

&lt;?php echo"&lt;a class='btn btn-primary' role='button' href='https://samplesite.com/result.php?" . http_build_query($listing) . "'&gt;See Listing&lt;/a&gt;"; ?&gt;

然后在结果页面上回显项目。

【讨论】:

  • 它是 role 而不是 roll。还有localhost,真的吗?
  • 是的......这是一个错字。是的,localhost 是一个快速填充文本。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多