Jump to content

PHP function getListingListLinks()


renfley

Recommended Posts

Hey guys ive got another fun one here.

 

I want to pull title, price, insertdate from database and display each listing like so

 

March 9:

 

123 bigwalkwayapartment - 900$

1455 little house must se!!!

 

March 8:

 

123 bigwalkwayapartment - 900$

1455 little house must se!!!

 

And so forth so basicly post the link by date.

 

function getListingListLinks(){//for the links
$ocdb=phpMyDB::GetInstance();
        $query="SELECT title, insertDate, price FROM ".TABLE_PREFIX."posts";
        $result=$ocdb->getRows($query);
Something??

         For Each Date 
             echo date;
            echo "<a href="#">title and price</a><br>;

}

 

I know im still far from completing my function but i def need the help!

 

Thanks

Link to comment
Share on other sites

Ok i don't know the class phpMyDB, anyway, you've now to fetch the query and print every information stored in the db

 

In a simple sql query this will be done in this way:

 

<?php

//Fetching:

$query="SELECT title, insertDate, price FROM ".TABLE_PREFIX."posts";

while($result=mysql_fetch_assoc($query))

{

        //Printing results:

      echo $result['title']." ".$result['insertDate']." ".$resul['price'];

}

?>

 

Sorry for my bad english :) Hope it helps!

Link to comment
Share on other sites

I think what you are trying to say is that you want the date to display one time and then all the entries for that date, then the next dat and those entries.

 

All you need to do is first order the results by date then create a "flag" variable to detect when the date changes to determine when to display the next date. You are apparently using a class for your query, so I'm not sure of the proper way to loop through the records. So, I just used a normal while() loop with mysql_fetch_assoc() for illustrative purposes. You will need to modify for your purposes.

function getListingListLinks(){//for the links
    $ocdb = phpMyDB::GetInstance();
    $query = "SELECT title, insertDate, price FROM ".TABLE_PREFIX."posts ORDER BY isertDate";
    $result = $ocdb->getRows($query);

    $output = '';
    $currentDate = false;
    while($row = mysql_fetch_assoc($result))
    {
        if($currentDate != $row['insertDate'])
        {
            $output .= "<br><b>{$row['insertDate']}</b><br><br>\n";
            $currentDate = $row['insertDate']
        }
        $output .= "<a href=''>{$row['title']}</a><br>\n";
    }
    return $output;
}

 

 

Also, you shouldn't echo within functions. Create the output and return the output to where the function is called. E.g.

echo getListingListLinks();

Link to comment
Share on other sites

So ive cleaned the code a bit but im not getting Any Results being printed!! In the index ive properly echo the Function but to no avail Any input?

 

function getListingListLinks(){//for the links
    $ocdb = phpMyDB::GetInstance();
    $query = "SELECT title, insertDate, price FROM ".TABLE_PREFIX."posts ORDER BY insertDate";
    $result = $ocdb->getRows($query);

    $output = '';
    $currentDate = false;
    while($row = mysql_fetch_assoc($result))
    {
        if($currentDate != $row['insertDate'])
        {
            $output .= "<br><b>{$row['insertDate']}</b><br><br>\n";
            $currentDate = $row['insertDate'];
        }
        $output .= "<a href=''>{$row['title']}</a><br>\n";
    }

    return $output;
}

 

Link to comment
Share on other sites

yup error is up and running  here i modified my code a little which works but doest give me the desired result!

 

function getListingListLinks(){//for the links
    $ocdb = phpMyDB::GetInstance();
    $query = "SELECT title, insertDate, price FROM ".TABLE_PREFIX."posts ORDER BY insertDate";


$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {
    echo $row['insertDate'];
echo $row['title'];
    echo $row['price'];
echo "<br>";
}   
   
}

 

Now this looks like the following.

 

2012-02-27 14:25:20Georgious 2 Bedroom Apartment Located in downtown Ottawa1250

2012-02-27 14:26:39Wow, Very clean 3Bdrm Apt. 750

2012-02-27 14:27:34Simple Loft lacated minutes from downtown.0

2012-02-27 14:28:29Spacious 3 Bdrm Ap located in orleans0

2012-02-27 14:30:08Huge 2Bdrm Apartment Available ASAP850

 

What i would like to achieve is this

 

March 12, 2012

Georgious 2 Bedroom Apartment Located in downtown Ottawa 1250$

Wow, Very clean 3Bdrm Apt. 750$

 

March 11, 2012

Spacious 3 Bdrm Ap located in orleans 0$

Huge 2Bdrm Apartment Available ASAP 850$

 

I am getting there but i just dont have the knowledge to get it to work the way i want. Now i did get part of my code from php.net and i am still looking but havnt found my answer as of yet!

 

Link to comment
Share on other sites

If someone is looking at this ive change the query from

 

$query = "SELECT title, insertDate, price FROM ".TABLE_PREFIX."posts Order BY insertDate";

 

$query = "SELECT title, insertDate, price FROM ".TABLE_PREFIX."posts GROUP BY insertDate";

 

REason for this is i want to display each title based on day so example today we have two ads i wanna show 2 ads if yesterday had on it would display the date and the single ad.

 

Can anyone confirm that group by would be better.

Link to comment
Share on other sites

I'm at a loss. I gave you the "logic" that would provide the solution you were after and specifically stated

You are apparently using a class for your query, so I'm not sure of the proper way to loop through the records. So, I just used a normal while() loop with mysql_fetch_assoc() for illustrative purposes. You will need to modify for your purposes.

 

You then respond that it doesn't work without making any of the changes necessary to work with the class you were using. Then, later, you change to use the built-in MySQL function and don't use the code I provided which would have worked. Not worth my time. Sorry.

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.