Solved

PHP if not in db echo

Posted on 2010-11-11
5
323 Views
Last Modified: 2012-06-27
Hello,

I am making a website for a magazine company and they have featured articles they publish every month.

They have been around for 5 years so there are a lot of articles that need to be inserted into the db.

What I would like to do is make it so that it will count from the first month ie '03/2005' to the current month ie 'MM/YYYY', create an array ie 03/2005, 04/2005, 05/2005 and check if those are in the db, if not then echo a list of the ones not in the db

It sounds really complex to me but I am assuming this is cake for some of you guys!

I would appreciate any help.

Here is the code I am using to currently echo the data.

<?php

$result = mysql_query("SELECT * FROM articles ORDER BY SUBSTRING(date,4) DESC, SUBSTRING(date, 1, 2) DESC")
or die(mysql_error());  

echo "<table border='0' width='100%'>";
echo "<tr> <td><strong>Date</strong></td> <td><strong>Title</strong></td> </tr>";
while($row = mysql_fetch_array( $result )) {
      // Print out the contents of each row into a table
      echo "<tr><td width='60px'>";
      echo $row['date'];
      echo "</td><td>";
      echo $row['title'];
      echo "</td></tr>";
}

echo "</table>";
?>



0
Comment
Question by:movieprodw
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:njovin
ID: 34113751
i = year
j = month
$i = 2005;
$j = 3;
while ($i <= 2010) {
 while ($j <= 12) {
 $x = 0;
$result = mysql_query("SELECT * FROM articles WHERE SUBSTRING(date,4) = '" . $i . "' and SUBSTRING(date, 1, 2) = '". $j ."'") 
or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
 echo "article title or whatever";
$x++;
}
if ($x == 0) {
 "No article for " . $j . "/" . $i;
}
 $j++;
 }
 $j = 1;
 $i++;
}

Open in new window

0
 
LVL 7

Expert Comment

by:karunamoorthy
ID: 34113767
could you pl post the table structure of articles table for further help.
0
 
LVL 1

Author Comment

by:movieprodw
ID: 34114227
hello njovin

It did not error but also did not output anything but
'article title or whateverarticle title or whatever '

karunamoorthy
the table structure is

[id]   [date]         [title]
1      mm/yyyy    'car article'
0
 
LVL 3

Accepted Solution

by:
njovin earned 500 total points
ID: 34114282
Right, it was meant for you to customize a bit. Try this:
$i = 2005;
$j = 3;
while ($i <= 2010) {
 while ($j <= 12) {
 $x = 0;
echo $j . "/" . $i . ": ";
$result = mysql_query("SELECT * FROM articles WHERE SUBSTRING(date,4) = '" . $i . "' and SUBSTRING(date, 1, 2) = '". $j ."'") 
or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
 echo $row['title'] . "<br />";
$x++;
}
if ($x == 0) {
 echo "No article <br />";
}
 $j++;
 }
 $j = 1;
 $i++;
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:movieprodw
ID: 34114301
Awesome!

Thank you so much
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TSQL Challenge... 7 37
Uploading a CSV Data Import via PHP & MySql 3 36
Echo Multiple values from multiple records 19 45
sql server query 12 26
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question