New Submissions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NaOH
Forum Newbie
Posts: 5
Joined: Sat Dec 22, 2007 8:31 pm

New Submissions

Post by NaOH »

scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi there,

I've got a photography portfolio, and I've written some code to query an SQL database for any photos submitted within the last week. The code is as follows:

Code: Select all

<?php 
$host="localhost";
$username="******"; 
$password="******"; 
$database="******"; 
$siteurl="http://www.oxidephoto.ca";
$connection = mysql_connect($host, $username, $password);
$db = mysql_select_db($database);
$q = "SELECT * FROM `plogger_pictures` WHERE DATE_SUB(CURDATE(),INTERVAL 14 DAY) <= date_submitted";
$result= mysql_query($q) or die
("Could not execute query : $q." . mysql_error());

while ($row=mysql_fetch_array($result))
{
$collection=$row["parent_collection"];
}

?>
This code exists within a while-db-has-pictures loop.

Also within this loop is the code to generate a 'NEW!' next to a new picture:

Code: Select all

<?php if ($collection == plogger_get_collection_id()) {echo "NEW! ";} ?>
This works, technically, but the problem that I'm having is it does it to the most recent photo only, and if there's any others that apply (if I submit three in a day or something), they aren't highlighted.

I reckon that the problem is because $collection gets overwritten by the newest item grabbed each time, but I don't know how to correct this. Any help would be greatly appreciated.

Thanks!


scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

the collection have to be an array

Post by yacahuma »

if you expect more than one result you have to do something like

Code: Select all

$collecttion = array();
while ($row=mysql_fetch_array($result))
{
$collection[]=$row["parent_collection"];
}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

yacahuma is dead on.

Right now, your $collection is a string that gets overridden each time the loop is iterated through.

Firstly, directly above the loop, declare $collection as an array.

Code: Select all

$collection = array();
Now you have a container to store stuff in when you're looping.

Inside of the loop, when you want to store something into $collection.. do:

Code: Select all

$collection[] = $row['parent_collection'];
The [] automatically creates a new element in the $collection array.

So when you're done with the loop, all of your values are stored in the $collection array.

You can see these values by doing:

Code: Select all

echo '<pre>'; print_r($collection); echo '</pre>';
You can also work with them by using any of the array functions or foreach().
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply