LITTLEBLACKDOG.COM Forum Index LITTLEBLACKDOG.COM

 
LWD LWD   FAQ FAQ   Memberlist Memberlist   Usergroups Usergroups   Active Topics Active Topics   Register Register  
  Profile Profile   Log in to check your private messages Log in to check your private messages   Log in Log in  
  Who is Online Who is Online   Image Gallery Image Gallery   Chat Chat   Search Search  
  LWDGear       LBDGear  

View next topic
View previous topic
Post new topic     Reply to topic   LITTLEBLACKDOG.COM Forum Index » Code Warriors
Author Message
drums
Toilet Drinker
Toilet Drinker


Joined: 19 Jun 2003
Posts: 232
Location: Portland, OR

Post Posted: Tue Oct 27, 2009 8:14 pm   Post subject: Need some PHP syntax with tags Reply with quote Back to top  

I have a program that allows my customers to update their web page by filling out an Excel spreadsheet I paid for years ago that I have been customizing for different purposes - calendars, content. I was hoping this app would work because sometimes in this particular application, the content will not have bullet points and since I didn't know how handle it I was hoping and praying this would work but it did not.
Code: Select all
<?php echo <h2> $record['header1']; </h2> ?>
<?php echo <p> $record['para1']; </p> ?>
 <blockquote><?php echo <li> $record['bullet1']; </li> ?>
          <li><?php echo $record['bullet1-2']; </li> ?>
            <?php echo <li> $record['bullet1-3']; </li> ?>
            <?php echo <li> $record['bullet1-4']; </li> ?>
            <?php echo <li> $record['bullet1-5']; </li> ?>
            <?php echo <li> $record['bullet1-6']; </li> ?>
            <?php echo <li> $record['bullet1-7']; </li> ?>
    </blockquote>
<?php echo <p> $record['para1-end']; </p> ?>


So I made it like this and it works but, because there are not always going to be bullets in the content, I need to either come up with a way to make it conditional or I can't offer this which is a big bummer.
Code: Select all
<h2><?php echo $record['header1']; ?></h2>
<p><?php echo $record['para1']; ?></p>
 <blockquote><li><?php echo $record['bullet1']; ?></li>
          <li><?php echo $record['bullet1-2']; ?></li>
            <li><?php echo $record['bullet1-3']; ?></li>
            <li><?php echo $record['bullet1-4']; ?></li>
            <li><?php echo $record['bullet1-5']; ?></li>
            <li><?php echo $record['bullet1-6']; ?></li>
            <li><?php echo $record['bullet1-7']; ?></li>
    </blockquote>
<p><?php echo $record['para1-end']; ?></p>

Can anyone lend a hand so that the line items and blockquote only shows up if there is data in there?

Thanks in advance!

_________________
View user's profile Send private message
Lycander
Lead Dog
Lead Dog


Joined: 24 May 2002
Age: 27
Posts: 12370
Location: The Constitution State

Post Posted: Wed Oct 28, 2009 1:32 am   Post subject: Reply with quote Back to top  

Code: Select all

<blockquote>
<?php
   foreach ($record as $bullet)
   {
      echo "<li>$bullet</li>\n";
   }
?>
</blockquote>


The foreach statement loops through the $record array without you having to know how many elements are in it before hand. If nothing is in $record the loop just bails.

_________________
Bright, Vibrant, Blue - Lycander's webcomic

Don't fwoosh me bro.
View user's profile Send private message
Lycander
Lead Dog
Lead Dog


Joined: 24 May 2002
Age: 27
Posts: 12370
Location: The Constitution State

Post Posted: Wed Oct 28, 2009 1:43 am   Post subject: Reply with quote Back to top  

Hmm, I see what you're doing there. Everything is just dumped into the $record array, things like 'header1', 'para1', and an arbitrary number of bullets.

Is there something that sets a maximum number of bullets allowed? I'd first run a quick loop to extract the bullets into a separate array, then my prior foreach code would work.

OOOOH!!! Just got an idea.

Code: Select all

<?php echo <h2> $record['header1']; </h2> ?>
<?php echo <p> $record['para1']; </p> ?>
<blockquote>
<?php
foreach($record as $key => $val)
{
   if( substr($key, 0, 6) == 'bullet')
   {
      echo "<li>$val</li>\n";
   }
}
?>
</blockquote>


What this does is loop through all the key/value pairs in the $record array, if it encounters a key that begins with "bullet" (to cover the undefined number of 'bullet1-#' keys) it'll print that element's value into a list item.

_________________
Bright, Vibrant, Blue - Lycander's webcomic

Don't fwoosh me bro.
View user's profile Send private message
drums
Toilet Drinker
Toilet Drinker


Joined: 19 Jun 2003
Posts: 232
Location: Portland, OR

Post Posted: Wed Oct 28, 2009 10:50 am   Post subject: I like what you're doing Reply with quote Back to top  

but the spreadsheet the data is pulled from has these headers so I know where to get the data from. each of the 5 possible entries has 7 bullets numbered like this: bullet1, bullet1-2, bullet1-3, bullet1-4, bullet1-5, bullet1-6, bullet1-7 would be the first event and then the next would start bullet2, bullet2-2, bullet2-3... and so on, so each bullet is unique. When I ran your script it repeats the first entry/entry all the way down the page. Plus I'm getting extraneous bullets. It might be better for you to visualize.
The hard coded page is here: http://www.youngberghill.com/youngberg_hill_calendar-events.html and the dynamic one is here: http://www.youngberghill.com/youngberg_hill_calendar-events.php
I was using this:
Code: Select all
<h2><?php echo  $record['header1']; ?></h2>
<p><?php echo $record['para1']; ?>
<blockquote>
<?php
foreach($record as $key => $val)
{
   if( substr($key, 0, 6) == 'bullet')
   {
      echo "<li>$val</li>\n";
   }
}
?>
</blockquote>
<?php echo $record['para1-end']; ?></p>


Thanks very much for your help!

_________________
View user's profile Send private message
drums
Toilet Drinker
Toilet Drinker


Joined: 19 Jun 2003
Posts: 232
Location: Portland, OR

Post Posted: Wed Oct 28, 2009 4:30 pm   Post subject: this worked Reply with quote Back to top  

I posted this at PHPFreaks and got something that did the job.
Code: Select all
<?php if(!empty($record['bullet1'])) echo "<li>".$record['bullet1']."</li>"; ?>
    <?php if(!empty($record['bullet1-2'])) echo "<li>".$record['bullet1-2']."</li>"; ?>
    <?php if(!empty($record['bullet1-3'])) echo "<li>".$record['bullet1-3']."</li>"; ?>
    <?php if(!empty($record['bullet1-4'])) echo "<li>".$record['bullet1-4']."</li>"; ?>
    <?php if(!empty($record['bullet1-5'])) echo "<li>".$record['bullet1-5']."</li>"; ?>
    <?php if(!empty($record['bullet1-6'])) echo "<li>".$record['bullet1-6']."</li>"; ?>
    <?php if(!empty($record['bullet1-7'])) echo "<li>".$record['bullet1-7']."</li>"; ?>

It would be cool to use something that used less code like what you were thinking but this works fine too...

_________________
View user's profile Send private message
Display posts from previous:   
Post new topic     Reply to topic

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group
phpBB SEO
All times are GMT - 8 Hours

Help us keep advertisements off this site. Donate today!