0

My index.php page has a display of listings from a json file which looks good. But I am trying to send a result of a single listing from that page to another page (result.php). How do I display the results of that single listing to the new page?

Here is the code for my first page that is attached to the json file:

<?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>

Here is the code to my second page that I want to pull from single listing of index.php file:

<?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>
Raptor
  • 53,206
  • 45
  • 230
  • 366
  • https://stackoverflow.com/a/36333831/3825777 https://stackoverflow.com/questions/42410352/how-to-decode-separate-php-encoded-json-in-jquery – react_or_angluar Jul 11 '21 at 02:12

2 Answers2

0

I might make a subtle change to the link to turn the ?id into a key/value pair, like

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

Then on the second page

<?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 -->
Kinglish
  • 23,358
  • 3
  • 22
  • 43
  • The code did not change the outcome of the result.php page. It always shows me the first object no matter which button on the index.php page I click. the link now shows "result.php?display=" plus the Oid of the property. The Oid is not the ID of the object. What code would I need to put in my result.php page to show the display from the index.php page? – McCarn Designs Jul 11 '21 at 03:42
  • `Oid` isn't unique? Is there an `id` property? We could use that. – Kinglish Jul 11 '21 at 03:45
  • make sure you're NOT using that loop `` on the listing detail page. The code I have in my answer should give you the `$listing` variable – Kinglish Jul 11 '21 at 03:47
  • The Oid is unique. My bad on that comment. Just confirming, when you're referring to the "Listings Page" are you talking about the index.php or the result.php page? – McCarn Designs Jul 11 '21 at 04:22
  • Yes, only talking about the result.php page. On the result page, there should be a GET variable for the oid, which I am calling $display. The array_filter code should weed out all items except the one you want, but it will come as an array with one item. Hence, the line $listing = $listing[0]; Note that I updated my answer to show that you want to get rid of that outside loop on the result page. – Kinglish Jul 11 '21 at 04:40
  • So, I updated the PHP code in the head of my result.php page and left the index.php PHP code alone except making the a link See Listing. I removed the $listings as $listing PHP code from the result.php page along with the code at the bottom of the code. However, this made it where no information is showing up from the json file now. – McCarn Designs Jul 11 '21 at 04:57
  • ok. I updated my answer. I forgot that the array_filter won't reindex the array, so I changed this line to: `$listing = array_values($listing)[0];` - you can see my test here: https://www.tehplayground.com/cioaokYXzB8oZRI3 --- hopefully that makes sense. The link is just a test of the array_filter and not a direct representation of your code – Kinglish Jul 11 '21 at 05:08
  • I fixed the problem by doing this to the button: See Listing"; ?> Then echoing the items on the results page. – McCarn Designs Jul 12 '21 at 01:41
0

I fixed the problem by doing this to the button:

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

Then echoing the items on the results page.