Jump to content

How to display information on another page


Skittle

Recommended Posts

Hi,

So I am currently making a real estate site for my class at school but I am having a tiny issue with the listing display page.

So what I want to do is show a bunch of listing that the agent owns on their dashboard which I have already done. Now I have two links one to update the listing and one to view the listing. Now what I am trying to do is when the view listing link is clicked it will take the user to the listing-display page and then I want it to display all the information for the listing that the link was clicked under. But i cannot for the life of me figure out how to do that!

 

This is currently the code for the dashboard where you only view the listing headline

<?php

// If the session was never set with a user id
$output = '';
$conn = db_connect();
if($_SESSION['userType'] != AGENT)
{
    $_SESSION['RedirectError'] = "You were not logged in as an Agent<br/>";
    header("Location:login.php");
}
if (isset($_GET["page"])) {
    $page  = $_GET["page"];
    $index = ($page -1) * IMAGE_LIMIT;
}
else {
    $page=1;
    $index = 0;
 }


?>
  <!-- start of main page content -->
  <div class="container" style="margin-top: 2em;">
    <h2>Dashboard</h2>

      <p>Welcome back <?php echo $_SESSION['userId']; ?> you last logged in on <?php echo $_SESSION['last_access']; ?></p>
      <h4>Your Listings</h4>
      <?php
          $sql = "SELECT listing_id FROM listings WHERE user_id = '".$_SESSION['userId']."' AND status = 'o' ORDER BY listing_id";
          $result = pg_query($conn, $sql);
          $listings = pg_fetch_all($result);
          for($index; $index < pg_num_rows($result); $index++)
          {
              $sql = "SELECT * FROM listings WHERE listing_id = '".$listings[$index]['listing_id']."'";
              $listing_result = pg_query($conn, $sql);
              $arrayRow = pg_fetch_assoc($listing_result);
              echo (build_listing_card($arrayRow));
              if($index !=0 && ($index +1) % IMAGE_LIMIT ==0){
                  break;
              }
          }
      ?>
          </div>
          <br/>
          <?php
          $total_pages = ceil(count($listings) / IMAGE_LIMIT);
          $pageLink = "<div class='pagination'>";
          for ($i=1; $i<=$total_pages; $i++) {
                       if($i == $page)
                       {
                          $pageLink .= "<a class='active' href='dashboard.php?page=".$i."'>".$i."</a>";
                       }
                       else {
                           $pageLink .= "<a href='dashboard.php?page=".$i."'>".$i."</a>";
                       }
          };
           ?>
       </div>
       <br/>

and this is the code for the listing-display page where I want all the listing information to be displayed
 

<?php

if (isset($_GET['listing_id']))
    {
        $_SESSION['listing_id'] = $_GET['listing_id'];
    }
    else
    {
        $_SESSION['listing_id'] = 0;
        echo "ERROR: listing information was not find";
    }

    $sql = 'SELECT * FROM listings WHERE listing_id = ' . $_SESSION['listing_id'];
    echo "<BR>".$sql;
    $result = pg_query(db_connect(), $sql);
    $records = pg_num_rows($result);

    echo "<BR>".pg_fetch_result($result, 0, "listing_id");
    echo "<BR>".pg_fetch_result($result, 0, "user_id");
    echo "<BR>".pg_fetch_result($result, 0, "status");
    echo "<BR>".pg_fetch_result($result, 0, "price");
    echo "<BR>".pg_fetch_result($result, 0, "headline");
    echo "<BR>".pg_fetch_result($result, 0, "description");
    echo "<BR>".pg_fetch_result($result, 0, "postal_code");
    echo "<BR>".pg_fetch_result($result, 0, "images");
    echo "<BR>".pg_fetch_result($result, 0, "city");
    echo "<BR>".pg_fetch_result($result, 0, "property_options");
    echo "<BR>".pg_fetch_result($result, 0, "bedrooms");
    echo "<BR>".pg_fetch_result($result, 0, "bathrooms");
    echo "<BR>".pg_fetch_result($result, 0, "garage");
    echo "<BR>".pg_fetch_result($result, 0, "purchase_type");
    echo "<BR>".pg_fetch_result($result, 0, "property_type");
    echo "<BR>".pg_fetch_result($result, 0, "finished_basement");
    echo "<BR>".pg_fetch_result($result, 0, "open_house");
    echo "<BR>".pg_fetch_result($result, 0, "schools");
?>
<h1> Listing Display </h1>
<hr/>
<table style="width:50%" border="1px solid black">
    <tr>
        <th>Listing Image</th>
        <th>Listing Description</th>
    </tr>
    <tr>
        <th rowspan="2">
            <img src="" alt="Oshawa House" width="300px" height="200px"/>
        </th>

        <td align="left" valign="top">
        <h3><?php echo pg_fetch_result($result, 0, "headline")  ?></h3>
        <ul>
        <li>Open house?: <?php echo get_property('open_house', pg_fetch_result($result, 0, "open_house"))?> </li>
        <li>Finished Basement?: <?php echo get_property('finished_basement', pg_fetch_result($result, 0, "finished_basement"))?></li>
        <li><?php echo get_property('purchase_type', pg_fetch_result($result, 0, "purchase_type"))?></li>
        <li>Garage Type: <?php echo get_property('garage_type', pg_fetch_result($result, 0, "garage"))?></li>
        <li>Near school?: <?php echo get_property('schools', pg_fetch_result($result, 0, "schools"))?></li>
        <li>Status: <?php echo get_property('listing_status', strtolower(pg_fetch_result($result, 0, "status"))) ?></li>
        <li>Washrooms: <?php echo get_property('washrooms',pg_fetch_result($result, 0, "bathrooms"))  ?></li>
        <li>Bedrooms: <?php echo get_property('bedrooms',pg_fetch_result($result, 0, "bedrooms"))  ?></li>
        <li>Description: <?php echo pg_fetch_result($result, 0, "description")  ?></li>
        </ul>
        </td>

    </tr>
    <tr>
        <td align="left" valign="top"><ul>
            <li>Location: <?php echo get_property('cities',pg_fetch_result($result, 0, "city"))  ?></li>
            <li>Postal Code: <?php echo pg_fetch_result($result, 0, "postal_code") ?></li>
            <li>Price: <?php echo pg_fetch_result($result, 0, "price") ?></li>
            </ul>
        </td>
    </tr>

</table>
<form action="listing-matches.php">
<p>
<input type="submit" name="submit" value="Back"/></p>
</form>

 

 

Link to comment
Share on other sites

Do not use the session to pass data between specific pages. Use the query string and $_GET. The session may look appealing but it has a number of drawbacks, such as being unable to view the same page with two different sets of data, and users being unable to copy or share URLs to a page.

Link to comment
Share on other sites

7 hours ago, Skittle said:

This is currently the code for the dashboard where you only view the listing headline

don't execute SELECT queries inside of loops. with today's server hardware, the time it takes for php to send the sql query statement to the database server is several times longer than the time it takes the database server to execute a query. you want to limit the number of queries you execute.

postgreSQL has a LIMIT n OFFSET m statement that you should use for pagination. if for some reason you are not supposed to use that for this assignment, use php's array_slice() on the $listings array to get an array of the desired ids. then use an IN() operator to get the desired rows of data using one query.

btw - the header() redirect needs an exit/die statement to stop program execution. without an exit/die, all the rest of that code is still executed.

since you haven't shown us  what the build_listing_card() code is or what output it produces, cannot help you with what or why your view listing links should be or don't work.

8 hours ago, Skittle said:

this is the code for the listing-display page where I want all the listing information to be displayed

don't write code like this either. every pg_fetch_result() call performs a data-seek, followed by a fetch. this takes twice as long a just fetching the data, and you have a couple of dozen pg_fetch_result() statements.

the query in this code will match at most one row of data. just fetch that row into a php variable, then access elements of that fetched array. this will run measurably faster and take a lot less typing to produce the code needed for the page.

if your initial long list of echo statements are for debugging purposes, just use print_r() or var_dump(), surrounded by html <pre>...</pre> tags to format the output. if this output is instead a desired part of the page output, don't spend your time typing out line after line of code for each possible field/column. use a loop and let php do the work for you.

  • Great Answer 1
Link to comment
Share on other sites

2 hours ago, mac_gyver said:

don't execute SELECT queries inside of loops. with today's server hardware, the time it takes for php to send the sql query statement to the database server is several times longer than the time it takes the database server to execute a query. you want to limit the number of queries you execute.

postgreSQL has a LIMIT n OFFSET m statement that you should use for pagination. if for some reason you are not supposed to use that for this assignment, use php's array_slice() on the $listings array to get an array of the desired ids. then use an IN() operator to get the desired rows of data using one query.

btw - the header() redirect needs an exit/die statement to stop program execution. without an exit/die, all the rest of that code is still executed.

since you haven't shown us  what the build_listing_card() code is or what output it produces, cannot help you with what or why your view listing links should be or don't work.

don't write code like this either. every pg_fetch_result() call performs a data-seek, followed by a fetch. this takes twice as long a just fetching the data, and you have a couple of dozen pg_fetch_result() statements.

the query in this code will match at most one row of data. just fetch that row into a php variable, then access elements of that fetched array. this will run measurably faster and take a lot less typing to produce the code needed for the page.

if your initial long list of echo statements are for debugging purposes, just use print_r() or var_dump(), surrounded by html <pre>...</pre> tags to format the output. if this output is instead a desired part of the page output, don't spend your time typing out line after line of code for each possible field/column. use a loop and let php do the work for you.

Um okay very helpful information but it doesnt answer my question

Link to comment
Share on other sites

12 minutes ago, Skittle said:

Um okay very helpful information but it doesnt answer my question

The question has been answered as detailed as we can from the information provided.

3 hours ago, mac_gyver said:

since you haven't shown us  what the build_listing_card() code is or what output it produces, cannot help you with what or why your view listing links should be or don't work

5 hours ago, requinix said:

Use the query string and $_GET.

10 hours ago, chhorn said:

You are fetching $_GET['listing_id'] from the URL, so you have to provide that info at least in a link

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.