[SOLVED]Simple if else satatement

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
FatherMerrin
Forum Newbie
Posts: 7
Joined: Fri Jul 04, 2008 6:11 pm

[SOLVED]Simple if else satatement

Post by FatherMerrin »

I'm having trouble here wondering if you could maybe drop me some knowledge.
if($_GET['type']=='latest'){

$where="CREATION_DATE > DATE_SUB(NOW(), INTERVAL 30 DAY)";

}elseif($_GET['type']=='old'){

$where="CREATION_DATE < DATE_SUB(NOW(), INTERVAL 30 DAY)";
I'm working on a PHP snippet... as you can see I have it so the stuff that is concidered new news is younger than 30 days and anything thats old news will be moved to the old news section once it has surpassed 30 days.

My quesiton is, how do I make is so that no matter what the creation date, I'll always have 2 items in the new news and the rest of the items in the old news sections.
Any bit of info will help me out.

or even possibly... have all the news under 30 days in the new news then moved to the old news once it has surpassed 30 days but make sure to leave at least 4 items remaining even if they are out of date if nothing else has been posted... I appreciate any help I can get and hope I posted this in the right spot. :D
Last edited by FatherMerrin on Mon Jul 14, 2008 2:13 pm, edited 1 time in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Simple if else satatement

Post by califdon »

You're in the right place. But you need to show us more of your code, since all that code is doing is assigning a value to the string $where. Presumably, you'll use that later in a SQL query, but that's what we need to see. (By the way, use [syntax=php]...[/syntax] tags, not [quote] tags for your code.) We will also need to see the code that you use to display news in the 2 sections (old and new).

Also, for us to give you any specific help, you'll need to make up your mind about the logic you want to implement. Either of the sets of logic you described can be implemented, but you have to tell us what you want.

Basically, the solution will be a combination of the WHERE clause and the LIMIT clause. I've done a similar thing with an events calendar, where they wanted to always show past events (shown in gray) for the past month, but never less than 6 events, even if they were older than a month.
FatherMerrin
Forum Newbie
Posts: 7
Joined: Fri Jul 04, 2008 6:11 pm

Re: Simple if else satatement

Post by FatherMerrin »

Hmm let me post the entire section of PHP that all this pertains to... hopefully you can get a bit of an understanding from this. Its all within a table layout

Code: Select all

<?
$sql=("SELECT * FROM ".DBTABLE_MYNEWS." WHERE CREATION_DATE > DATE_SUB(NOW(), INTERVAL 30 DAY) ORDER BY CREATION_DATE DESC");
$lo_Mynews = mysql_query($sql) or die("Error.<br>".mysql_error()."<br>".$sql);
 
for ($i = 0;$i < mysql_num_rows($lo_Mynews); $i++){
  $myImage = mysql_result($lo_Mynews, $i, "IMAGE");
  $text=substr(stripslashes(mysql_result($lo_Mynews, $i, "ARTICLE")),0,125);
  $split=explode(" ",$text);
  $count=count($split);
  for($n=0;$n<($count-1);$n++){
    $new_text.=$split[$n]." ";
  }
?>
This is the snippet of what I'm using for the new news frame and the same is true for the old news frame only the > is reversed ofcourse.
after this statement is when I actually draw the info on screen and in what order.
Let me know if you need anything else to work with.

Suppose the desired layout is the second option I posted...
... have all the news under 30 days in the new news then moved to the old news once it has surpassed 30 days but make sure to leave at least 4 items remaining even if they are out of date if nothing else has been posted...
As always any little bit of info will help me out. :wink:
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Simple if else satatement

Post by califdon »

I think you can do what you want to do even more simply. Try this:

Code: Select all

<?
$currDate = mktime(0,0,0,date("m"),date("d"),date("Y"));
$oldDate = $currDate - (60 * 60 * 24);
 
$sql=("SELECT * FROM ".DBTABLE_MYNEWS." ORDER BY CREATION_DATE DESC");
$Mynews = mysql_query($sql) or die("Error.<br>".mysql_error()."<br>".$sql);
 
// Any formatting for the header of 'New News' goes here
 
$oldCount = 0;
while($row=mysql_fetch_array($Mynews) && $oldCount < 5) {
   // Cycle through records in Descending date order:
   if(CREATION_DATE > $oldDate) {
 
      // Format your data for New news here
 
   } else {  // now we're into older than 'old date':
      if($oldCount == 0) {  first "old" record, output header:
 
         // Format the header of 'Old News'
 
      }
 
      $oldCount+;  // start counting how many old records:
 
      // Format your data for Old news here
 
   }
}
?>
I think that should do what you want, if I understood your description properly. That should put everything newer than 30 days ago into the "new" category, followed by no more than 4 items in the "old" category. If that's not exactly what you need, you may be able to see how to change the logic to meet your requirements.

Feel welcome to return if this still doesn't give you what you want. And if it does, please return to your first posting and EDIT the Subject to include the word [SOLVED] to save the time of those who are looking for people who still need answers.
FatherMerrin
Forum Newbie
Posts: 7
Joined: Fri Jul 04, 2008 6:11 pm

Re: Simple if else satatement

Post by FatherMerrin »

I hate to admit it, but this is just a bit beyond my knowledge of the syntax needed afterward. I'm not sure of how I would display the info using PHP in a neat and orderly way, which is why I used the 2 seperate things for "New" and "Old" posting so I could assign variables for each to display later. It allows me to use HTML to setup the layout of the results and just PHP snippets to augment a frame to have the ability to pull from the database.

Is there maybe some way I can get you to show me an example of how to do it as 2 seperate places instead of an If, ElsIf?

On a side note, I wrote the logic display wrong on my previous post. What I meant to say was I want to keep only 2 "New" items and all the rest in "Old".

You've been very helpful by giving me a few more topics to research. I appreciate all the help you've givin me so far :D .
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Simple if else satatement

Post by califdon »

FatherMerrin wrote:I hate to admit it, but this is just a bit beyond my knowledge of the syntax needed afterward. I'm not sure of how I would display the info using PHP in a neat and orderly way, which is why I used the 2 seperate things for "New" and "Old" posting so I could assign variables for each to display later. It allows me to use HTML to setup the layout of the results and just PHP snippets to augment a frame to have the ability to pull from the database.

Is there maybe some way I can get you to show me an example of how to do it as 2 seperate places instead of an If, ElsIf?

On a side note, I wrote the logic display wrong on my previous post. What I meant to say was I want to keep only 2 "New" items and all the rest in "Old".

You've been very helpful by giving me a few more topics to research. I appreciate all the help you've givin me so far :D .
Why don't you describe how you want it to be displayed, then. The correction on the logic actually makes it even simpler!

Just show me an example of exactly how you would like it to display. If you will enclose it in [ text] and [ /text] tags, it will preserve multiple spaces in the layout. (I inserted a space in the tags in the line above, to prevent it from actually interpreting them.)
FatherMerrin
Forum Newbie
Posts: 7
Joined: Fri Jul 04, 2008 6:11 pm

Re: Simple if else satatement

Post by FatherMerrin »

I thought for a bit and decided the best way I can show what I'm trying to get as my final result is to just show you a picture of generally what it is.

Image

If this pic doesn't work out for what I'm aiming for just let me know what you need code wise and we can maybe work something through.
There is no need to mess with the labels above the boxes... its just to let you know which is which. I have both of these frames in seperate spots so that I can just insert the PHP in a designated open frame. I'm not sure how well you can see it but the greyed boxes will be my pictures that are loaded through my admin panel as with the other sections ofcourse.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Simple if else satatement

Post by califdon »

Before I spend any more time on the code, let me make sure I understand you. Your picture shows two columns, the left column for "New" items, the right column for "Old" items, but you said you only wanted to ever have 2 new items, and everything else as old. Is that true? You show 4 items in the left column in your picture. If that's merely an older picture, fine, but I want to know exactly what you want. A left column that ALWAYS has precisely TWO items, no more, no less. Right? And everything else in the right column? So there's no more "30 days" or anything? That's pretty easy to do, but I'm not going to start until I know that that's what you're trying to achieve. The thing about programming is, you can't take anything for granted. Computers are dumb. They will do just what you tell them to do, which often is not the same as what you want them to do!
FatherMerrin
Forum Newbie
Posts: 7
Joined: Fri Jul 04, 2008 6:11 pm

Re: Simple if else satatement

Post by FatherMerrin »

I'm sorry for not clarifying... yes I aiming for there to always be 4 items in the Latest News article even if they are out of date (suppose that would elliminate the 30 days thing yes) and all the rest of the news to be on old news.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Simple if else satatement

Post by califdon »

FatherMerrin wrote:I'm sorry for not clarifying... yes I aiming for there to always be 4 items in the Latest News article even if they are out of date (suppose that would elliminate the 30 days thing yes) and all the rest of the news to be on old news.
FatherMerrin wrote:On a side note, I wrote the logic display wrong on my previous post. What I meant to say was I want to keep only 2 "New" items and all the rest in "Old".
So which is it, two or four???????
FatherMerrin
Forum Newbie
Posts: 7
Joined: Fri Jul 04, 2008 6:11 pm

Re: Simple if else satatement

Post by FatherMerrin »

Its 4 I'm looking for. Sorry it was a quick post the first time.
So that if another new topic were posted the oldest of the 4 would move to the "Old" section and the new one would replace it leaving 4.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Simple if else satatement

Post by califdon »

Well, I don't have time to test this, but this should be pretty close to what you want:

Code: Select all

. . .
<head>
 . . .
<style type="text/css">
   span.header {
      font-weight:bold;
      color:#00F000;
   }
</style>
 . . .
</head>
<body>
 . . .
<?
$sql=("SELECT * FROM ".DBTABLE_MYNEWS." ORDER BY CREATION_DATE DESC");
$Mynews = mysql_query($sql) or die("Error.<br>".mysql_error()."<br>".$sql);
?> 
   <table>
      <tr>
         <td width='50%'><span class='header'>Latest News Section:</span><br />
<?php
$myCount = 0;
while($row = mysql_fetch_array($Mynews)) {
 
   // Cycle through records in Descending date order:
   if(myCount < 4) {
         // This should be in column one:
      echo $your_text_from_table . "<br />";
      echo "<a href='$your_more_link'>...see more</a><br />";
 
   } else { 
      if(myCount == 4) {  // Need header for second column:
         echo "</td><td width='50%'><span class='header'>Old News Section:</span><br />
      }
 
         // This should be in column two:
      echo $your_text_from_table . "<br />";
      echo "<a href='$your_more_link'>...see more</a><br />";
 
   }
 
   $myCount+;  // increment count
}
?>
         </td>
      </tr>
   </table>
 
 
 
 
 
 
See if you can get this to basically work for you. If it doesn't, please be explicit in what is or is not working.

This won't be the fanciest display in the world (it could probably be done much more nicely using CSS rather than an HTML table, but that would require considerably more time, at least for me), but the important thing is to get it delivering what you want, where you want it. You can make it prettier later.

Oh, and of course, I don't know what fields you have that you want to display, or that contain your links, so I have used pseudo names in there. Obviously, it won't work until you substitute the names in your database.
FatherMerrin
Forum Newbie
Posts: 7
Joined: Fri Jul 04, 2008 6:11 pm

Re: Simple if else satatement

Post by FatherMerrin »

After mulling it over and comparing what you had written to the previous work I had, I realized its much more simple to fix with my current code than I first though.

by removing the
WHERE CREATION_DATE > DATE_SUB(NOW(), INTERVAL 30 DAY) from $sql

and modifying the count (my logic for i ) to be

Code: Select all

for ($i = 0;$i < 4; $i++);
instead of

Code: Select all

for ($i = 0;$i < mysql_num_rows($lo_News);
I now have only 4 items in the Latest News section.

Now my question is... how would I modify that same line in the Old News section to display every news item except the first 4 (so that I'm not doubling up on stories)
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Simple if else satatement

Post by califdon »

I simply wouldn't do it that way, having two "sections". There are no "sections" in your logic, thus there should be none in your code. You simply want to display the first 4 records one place, and the rest of them someplace else. That only takes one query and a record counter. That's what I gave you in my previous response. If you are familiar with CSS, you could do a better job than I did, putting the first 4 records into one <div> and the rest into another <div>, but that's all it takes.
Post Reply