Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need assistance with $CharacterLimit function in sql/php

Status
Not open for further replies.

Dreamtripper

Programmer
Dec 8, 2006
3
US
Hello, I need assistance inserting $CharacterLimit in to my code so that the called information does not explode my tables. The function needs to be inserted into the code below. And <?=$name?> is what I am wanting to limit to 14 characters.
----------------------
<?php

include("includes/profile.class.php");
$profile=new profile;

include("includes/people.class.php");
$people=new people;

$new_members=$profile->get_new_people();
while($new_set=mysql_fetch_array($new_members))
{
$num_images=$people->get_num_images($new_set["member_id"]);
if($num_images==0)
{
$gender=$people->check_gender($new_set["member_id"]);
if($gender=="Male")
{
$image="<img alt='' src='images/male.gif' width=80 border=0>";
}
else
{
$image="<img alt='' src='images/female.gif' width=80 border=0>";
}
}
else
{
$image_url=$people->get_image($new_set["member_id"]);
$pic_name=str_replace('user_images/', '', $image_url);
$image = "<img src='image_gd/image.php?$pic_name' border='0'>";
}
$people_info=$people->get_info($new_set["member_id"]);
$profile_info=$people->get_profile($new_set["member_id"]);
$name=$people->get_name($new_set["member_id"]);
$CharacterLimit=14; // Define how many characters to be displayed
?>
<!-- person 1 -->
<td width="320" height="100" align="center" valign="top" bgcolor="#FFFFFF" class=""><p><a href="view_profile.php?member_id=<?=$new_set["member_id"]?>" id="coolNewPersonURLa1"></a> <a href="view_profile.php?member_id=<?=$new_set["member_id"]?>">
<?=$name?>
</a> </p>
<?=$image?>
<p><a href="view_profile.php?member_id=<?=$new_set["member_id"]?>"> </a>
<?php
----------------------------
Any help would be appreciated as I am a beginner and desperately need this function. I checked out another post here ( and I am confused by the term echo, as my script does not use that.

Thanks in advance
 
Try:
Code:
<?
$CharacterLimit=30;
$limited_name=substr($name, 0, $CharacterLimit);
echo $limited_name;
?>
and juts so you know echo is the explicit way for outputting data to the browser.

so this:

Code:
<?=$somevriable?>

is exactly the same as
<?
echo $somevariable;
?>






----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Okay I added it, and kind of works, Here is the code after I put in the code you suggested:

<?php

include("includes/profile.class.php");
$profile=new profile;

include("includes/people.class.php");
$people=new people;

$new_members=$profile->get_new_people();
while($new_set=mysql_fetch_array($new_members))
{
$num_images=$people->get_num_images($new_set["member_id"]);
if($num_images==0)
{
$gender=$people->check_gender($new_set["member_id"]);
if($gender=="Male")
{
$image="<img alt='' src='images/male.gif' width=80 border=0>";
}
else
{
$image="<img alt='' src='images/female.gif' width=80 border=0>";
}
}
{
$image_url=$people->get_image($new_set["member_id"]);
$pic_name=str_replace('user_images/', '', $image_url);
$image = "<img src='image_gd/image.php?$pic_name' border='0'>";
}
$people_info=$people->get_info($new_set["member_id"]);
$profile_info=$people->get_profile($new_set["member_id"]);
$name=$people->get_name($new_set["member_id"]);

$CharacterLimit=14;
$limited_name=substr($name, 0, $CharacterLimit);
echo $limited_name;

?>
<!-- person 1 -->
<td width="320" height="100" align="center" valign="top" bgcolor="#FFFFFF" class=""><p><a href="view_profile.php?member_id=<?=$new_set["member_id"]?>" id="coolNewPersonURLa1"></a> <a href="view_profile.php?member_id=<?=$new_set["member_id"]?>">
<? echo $name;?>
</a> </p>
<?=$image?>
<p><a href="view_profile.php?member_id=<?=$new_set["member_id"]?>"> </a>
<?php

---------------------------
And if you will look at you will see the output that it gives me in the newest members column.
 
Ah I got it! Just need to change this <? echo $name;?> to <?=$limited_name?> Works great. Now is there away to add ... at the end for names that are longer than 14 characters?

And thanks vacunita for your fast assistance.
 
Of course, just add some logic to it:
Code:
if (strlen ($name) >= $CharacterLimit)
{
  $name = substr ($name, 0, $CharacterLimit) . '...';
}
echo $name;
If $name is longer than the number of characters specified in $CharacterLimit, it will cut it at that mark and add three dots. If not, it will simply output the regular $name.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top