Controlling field data

Hi everyone, hope you can help.

I have 2 questions which i believe are closely related, so i will ask them in one post if that’s ok.

First question: Is it possible to control the length of a field output when echoing a field with alot of information in it. This is my code <dt><span class="model"><?php echo $f2; ?>&nbsp;<?php echo $f7; ?></span></dt>

Field 7 contains a very long description of which i only want to use the first word (in this instance anyway)
Editing the database is not an option, so id there anyway i can limit the output to just the first word ?

The next question is very similar … I have another field of image URLS with each url in the column seperated by a comma ,
When i echo the field it obviously displays returns all URLS as one very long URL … So is it possible to only display the URL upto but not including the first comma. Again editing the database is not an option.

Hope someone can help with either of the above, thanks

The solution is the same for both but instead of a space change the needle to a comma.

[php]
// php 5.3
$string = ‘aaa bbb ccc ddd eee’;
echo strstr($string, ’ ', true);

// older versions
$string = ‘aaa bbb ccc ddd eee’;
echo substr($string, 0, strpos($string, ’ '));
[/php]

Thank you very much for your reply, unfortunately i can’t get this to work, although that is no real surprise, as i am an absolute novice.

Could you please show me an example of how i would apply this to the following code.

[php]

<?php echo $f2; ?> <?php echo $f7; ?>
[/php]

Basically i just want the $f7 to only show the first word of the data in the column.

Thanks again for your help.

Hi there, i have been playing around with this example and still cannot get it to work. I would greatly appreciate any help.

Basically i want to control the length of string produced here
[php]

<?php echo $f9; ?>

[/php]

If i do this it obviously works fine
[php]

<?php $string = ‘aaa bbb ccc ddd eee’;
echo strstr($string, ’ ', true); ?>

[/php]

Displays aaa

My problem is i don’t want to physically enter the $string, but instead call it from a database, as in the first example.
So how can i replace aaa bbb ccc ddd eee for <?php echo $f9; ?>

Thanks again for any help

$string is just an example, of course you would use your own string there

[php]

<?php echo strstr($f2, ' ', true); ?> <?php echo strstr($f7, ' ', true); ?>
[/php]

When you put it like that i now kinda feel a little stupid lol … That is exactly what i wanted !!! Thank you very much

Just out of interest, obviously this works by detecting a space or whatever is put between the needles, but can this be used in a similar way to select the third or fourth word for example … Just thought i would ask for the benefit of learning something new.

Thanks again for your help.

These particular functions would not work for that. However you could use explode()

[php]
$string = ‘aaa bbb ccc ddd eee’; // don’t forget this is just an example string :slight_smile:
$parts = explode(’ ', $string);
echo $parts[2]; // output is ‘ccc’
[/php]

This function could also be used for your first solution (using $parts[0]) but it’s much slower if you have a very large string. You could even use it to get a random key

[php]
$string = ‘aaa bbb ccc ddd eee’;
$parts = explode(’ ', $string);
echo $parts[rand(0, (count($parts) - 1))]; // output is random
[/php]

Wow m@tt … this is fantastic information and it is really allowing me to experiment. I must remind you that i am an absolute novice at PHP, but basically i am building a little experiment and using what i learn to do so. It’s totally unconventional in my methods and extremely badly coded on my part, but it works and it really is just for fun at the moment.
If i am stretching this thread out too long, please say so, but i would like to expand on your previous and show you how i have implemented it. The following works in theory but there is maybe a better way of doing it.
I have used you previous example to display images from a database. The image URLs are all in the same column separated by a comma. The following example works, if 6 images exist, but obviously if only 5 exist it shows the description as produced by alt="".
What i would like to do is produce 1 image placeholder with php code and loop it for every image it finds instead of having 6 pre assigned place holders (incorrect terminology i know) so here is how it currently works.

[php]

						<ul class="thumbs list-image clearfix">

							<li>
								<a class="thumb" name="leaf" href="<?php $parts = explode(',', $f1); echo $parts[0]; ?>" title="<?php echo $f2; ?>">
									<img src="<?php $parts = explode(',', $f1); echo $parts[0]; ?>" alt="<?php echo $f2; ?>" style="width:146px; height:88px;"/>
								</a>
							</li>

							<li>
								<a class="thumb" name="leaf" href="<?php $parts = explode(',', $f1); echo $parts[1]; ?>" title="<?php echo $f2; ?>">
									<img src="<?php $parts = explode(',', $f1); echo $parts[1]; ?>" alt="<?php echo $f2; ?>" style="width:146px; height:88px;"/>
								</a>
							</li>

							<li>
								<a class="thumb" name="leaf" href="<?php $parts = explode(',', $f1); echo $parts[2]; ?>" title="<?php echo $f2; ?>">
									<img src="<?php $parts = explode(',', $f1); echo $parts[2]; ?>" alt="<?php echo $f2; ?>" style="width:146px; height:88px;"/>
								</a>
							</li>
							<li>
								<a class="thumb" name="leaf" href="<?php $parts = explode(',', $f1); echo $parts[3]; ?>" title="<?php echo $f2; ?>">
									<img src="<?php $parts = explode(',', $f1); echo $parts[3]; ?>" alt="<?php echo $f2; ?>" style="width:146px; height:88px;"/>
								</a>
							</li>

							<li>
								<a class="thumb" name="leaf" href="<?php $parts = explode(',', $f1); echo $parts[4]; ?>" title="<?php echo $f2; ?>">
									<img src="<?php $parts = explode(',', $f1); echo $parts[4]; ?>" alt="<?php echo $f2; ?>" style="width:146px; height:88px;"/>
								</a>
							</li>

							<li>
								<a class="thumb" name="leaf" href="<?php $parts = explode(',', $f1); echo $parts[5]; ?>" title="<?php echo $f2; ?>">
									<img src="<?php $parts = explode(',', $f1); echo $parts[5]; ?>" alt="<?php echo $f2; ?>" style="width:146px; height:88px;"/>
								</a>
							</li>

						
						</ul><!--/ .thumbs-->

					</div><!--/ #thumbs-->[/php]

Could you advise of a tidier way of producing a similar result, but only show 2 if 2 exist or 10 if 10 exist ?
Thanks again for all your help !!

You don’t need to explode every time. You just do it once and reference the same array.

Just create a loop:

[php]

    <?php $parts = explode(',', $f1); foreach($parts as $part) { ?>
  • <?php echo $f2; ?>
  • <?php } ?>
[/php]

Thank you so very much, that is absolutely awesome, works perfectly !!
Absolutely amazing support here !!
I have many more questions but i think i have exhausted this thread now, thanks you once again !!

Sponsor our Newsletter | Privacy Policy | Terms of Service