PHP question or RFH (Request for help)

Discuss Programming
Post Reply
byrdman
administrator
administrator
Posts: 225
Joined: Thu May 08, 2003 1:59 pm
Location: In the cloud

PHP question or RFH (Request for help)

Post by byrdman » Fri Sep 11, 2009 1:59 pm

I am parsing a XML feed and I am inputting the results into MySQL.

right now I have the following:

Code: Select all

<![CDATA[
                        Former Sen. Norm Coleman has been diagnosed with
Bell's Palsy, a facial paralysis that is typically temporary and caused by
still unknown factors. Coleman lost his bid for a second term in Congress late
June, when the Minnesota Supreme Court ruled in favor of Sen. Al Franken in
the state's closest and most contentious Senate race.
                        <div>
                           Blah Blah Blah feed - All Rights Reserved
                        </div>
                    ]]>
my php code grabs everything after CDATA[ and before the first <div> tag. The problem is there is a line break and 25 spaces between the CDATA[ and the first letter of the feed. This line break and spaces also gets inserted into my database.

How can I ignore the spaces and line break?

User avatar
Void Main
Site Admin
Site Admin
Posts: 5712
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Fri Sep 11, 2009 2:27 pm

What you need is a little trim:

http://www.php.net/trim

After parsing and right before your INSERT it should be as simple as:

$var = trim($var)

byrdman
administrator
administrator
Posts: 225
Joined: Thu May 08, 2003 1:59 pm
Location: In the cloud

Post by byrdman » Fri Sep 11, 2009 3:48 pm

muchas gracias!!

byrdman
administrator
administrator
Posts: 225
Joined: Thu May 08, 2003 1:59 pm
Location: In the cloud

Post by byrdman » Tue Sep 15, 2009 2:10 pm

You have been so helpful, I though I would try again :)

my initial thinking started with this:

Code: Select all


$days = array("mon", "tues", "wed", "thurs","fri","sat","sun");
        foreach($days as $num => $day) {
          echo ($num+1) . " " . $day ."</br>"; 
since today is tuesday, php returns today as 2 when i do :

Code: Select all

$today = date("N");
echo $today.
my initial php file grabs a forecast from a weather feed and places it into mysql into the fields (monday_name, monday_conditions, monday_high, monday_low...same 4 fields foreach day of the week.

how can I get it so when I pull the data it puts it in order based on what today is?

for example

since today is tuesday(2),
I can pull
wednesday(3), thursday(4), friday(5), saturday(6), sunday(7), monday(1)

if today was wednesday (3)
it would look like:
thursday(4), friday(5), saturday(6), sunday(7), monday(1), tuesday(2)

User avatar
Void Main
Site Admin
Site Admin
Posts: 5712
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Tue Sep 15, 2009 7:34 pm

There are many ways to do it. Here is the first thing that came to mind (there are surely easier ways):

Code: Select all

<?
   echo "Next 6 days including today:\n";

   $now = time();
   for ($i=0;$i<6;$i++) {
      $nextday = date('N',strtotime("+$i day", $now));
      echo "Day: $nextday\n";
   }
?>
Or:

Code: Select all

<?
   echo "Next 6 days including today:\n";

   $today = date("N");
   for ($i=0;$i<6;$i++) {
      $day = $today + $i;
      if ($day > 7) {
         $day -= 7;
      }
      echo "Day: $day\n";
   }
?>

byrdman
administrator
administrator
Posts: 225
Joined: Thu May 08, 2003 1:59 pm
Location: In the cloud

Post by byrdman » Tue Sep 15, 2009 9:41 pm

Thank you, it worked!! But my idea may not be possible after I constructed it
THis is the output:

Code: Select all

<day1>$day3name</day1>
<day1condition>$day3cond</day1condition>
<day1image>$day3image</day1image>
<day1high>$day3high</day1high>
<day1low>$day3low</day1low>
<day2>$day4name</day2>
<day2condition>$day4cond</day2condition>
<day2image>$day4image</day2image>

<day2high>$day4high</day2high>
<day2low>$day4low</day2low>
It is exactly how I wanted it to look.

But my code that generates that looks like :

Code: Select all


<?php

  for ($i=1,$j=1;$i<7,$j<7;$i++,$j++) {
     $nextday = date('N',mktime(0, 0, 0, date('m'), date('d') + $i, date('Y')));
     $curday = $j;
     echo "<day" . $curday . ">\$day" . $nextday . "name</day" . $curday . ">\n";
     echo "<day" . $curday . "condition>\$day" . $nextday . "cond</day" . $curday . "condition>\n";
     echo "<day" . $curday . "image>\$day" . $nextday . "image</day" . $curday . "image>\n";
     echo "<day" . $curday . "high>\$day" . $nextday . "high</day" . $curday . "high>\n";
     echo "<day" . $curday . "low>\$day" . $nextday . "low</day" . $curday . "low>\n";

  }

?>

So my $nextday is returning a number that should make another varable when the php is run. Instead of getting $day3low from the database, it is just printing "$day3low"

User avatar
Void Main
Site Admin
Site Admin
Posts: 5712
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Tue Sep 15, 2009 10:12 pm

I made some changes to my previous post for a couple better ways to get the numbers you were looking for. As far as what you are trying to do, you should be able to do it. I almost need to see more of your code to see what you are trying to do and where things are going wrong.

I actually think you would be better off using arrays in this case. For example condition in an array:

Code: Select all

<?php

  $cond[1] = "cloudy";
  $cond[2] = "partly cloudy";
  $cond[3] = "rain";
  $cond[4] = "sunny";
  $cond[5] = "partly sunny";
  $cond[6] = "fog";
  $cond[7] = "clear";

  $today = date("N");
  for ($i=1;$i<7;$i++) {
     $nextday = $today + $i;
     if ($nextday > 7) {
         $nextday -= 7;
     }
     echo "<day" . $i . ">" . $cond[$nextday] . "</day" . $i . ">\n";
     echo "<day" . $i . "condition>\$day" . $nextday . "cond</day" . $i . "condition>\n";
     echo "<day" . $i . "image>\$day" . $nextday . "image</day" . $i . "image>\n";
     echo "<day" . $i . "high>\$day" . $nextday . "high</day" . $i . "high>\n";
     echo "<day" . $i . "low>\$day" . $nextday . "low</day" . $i . "low>\n";

  }

?>

Post Reply