1

I work with football players, but players like Oxlade-Chamberlain are far to big to show on a card, so I want to change the font size when there are more then 15 characters. I use this

<?php 
  $sql = "SELECT * FROM `15players` WHERE `id`=" . $row['id'] . ""; 
  $result = mysql_query($sql) or die('Query failed: ' . mysql_error());
  $row = mysql_fetch_array($result);
  if($row['Common_Name'] === NULL)
    echo  $row['Last_Name'];
  else
    echo $row['Common_Name'];
  if ($row['Common_Name'] > 14 )
     ?>

So I want to change if ($row['Common_Name'] > 14 ) to something that can change my font-size when it is to big ( more than 14 characters)

Like Schweinsteiger fits perfectly (http://jterweele.informatica.bc-enschede.nl/speler.php?id=823) but Oxlade-Chamberlain is messing up everything (http://jterweele.informatica.bc-enschede.nl/speler.php?id=6997)

EDIT: I found the answer.

Julan tW
  • 23
  • 4
  • Possible duplicate of [font-size depending on character length?](http://stackoverflow.com/questions/9045390/font-size-depending-on-character-length) – davejal Nov 04 '15 at 22:38
  • I agree it's a duplicate, but the accepted answer to the question is poor at best. – blm Nov 04 '15 at 22:40
  • @blm I don't think it's poor. I actually was going to suggest the same function, however there are a few ways to achieve what the OP wants to do here. – Funk Forty Niner Nov 04 '15 at 22:48
  • @Fred-ii- Embedding presentationy things like font sizes in server side code is poor. Use a class and put the font size in the css. – blm Nov 04 '15 at 22:50
  • @blm No argument there. However, it's the logic that counts `if(strlen($div_string)>$random_number)`. The rest is irrelevant. – Funk Forty Niner Nov 04 '15 at 22:52
  • 2
    Keep in mind that "WWW" is much wider than "iii". One way I have seen this solved is to create a div in a browser without appending it, add the text, and measure the width. – James Nov 04 '15 at 22:57
  • @Fred-ii- Is an answer that's partly correct and partly suggesting a really bad practice a good answer? I'd say no. People who don't know any better are going to copy the code from the answer verbatim. – blm Nov 04 '15 at 23:00
  • `SELECT * FROM table WHERE CHAR_LENGTH(field) > 14` (or `LENGTH()`) set in a conditional statement. – Funk Forty Niner Nov 04 '15 at 23:03
  • @blm You're more than welcome to submit an answer. I myself will have to pass on this one, since this question seems to pull more towards *"primarily opinion-based"*. Personally, I'd use a MySQL method, but that's just me. – Funk Forty Niner Nov 04 '15 at 23:06

2 Answers2

3

i would suggest wrapping a <span> around the text and then setting up a class

if($row['Common_Name'] === NULL)
{
    echo $row['Last_Name'];
}
else
{
    // since you are outputting $row['Common_Name'] to the page i am assuming 
    // it's a string and if you want to check the length you want strlen
    if (strlen($row['Common_Name']) > 14 )
    {
        echo "<span class='smaller'>".$row['Common_Name']."</span>";
    }
    else
    {
        echo $row['Common_Name'];
    }
}

the code above should serve your purpose but if you want to improve on it you could do something like this

$name = "";
if($row['Common_Name'] === NULL)
{
    $name = $row['Last_Name'];
}
else
{
    $name = $row['Common_Name'];
}

echo "<span class='";
$nameLen = strlen($name);
switch (true) 
{
    case $nameLen > 14:
        echo "smaller";
        break;
    case $nameLen > 20:
        echo "evenSmaller";
        break;
    default:
        echo "normal";
}
echo "'>".$name."</span>";

with this code regardless if $row['Last_Name'], $row['Common_Name'] or whatever else is chosen (as last names can be very long by themselves, look at Apu's on The Simpsons) you can adjust size the for multiple situations.

the reason why i store the length in a variable is for 2 reasons

  1. if you need to manipulate the length value because of special situations (ie. if the last name was McDonald and Capital Letters are set to be displayed to be twice as big as regular letters and you want to add +1 to the length for every capital letter)

  2. the rare event where strlen() became depreciated for some reason and you need it changed to a new function or even if you want to use your own strlen() function, with this code you only need to change it in once place

Memor-X
  • 2,870
  • 6
  • 33
  • 57
-1

You can do this with jQuery:

1.Firstly include jQuery library:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

2.Secondly use this code:

<?php $sql = "SELECT * FROM `15players` WHERE `id`=" . $row['id'] . ""; 
    $result = mysql_query($sql) or die('Query failed: ' . mysql_error());
         $row = mysql_fetch_array($result);
         if($row['Common_Name'] === NULL)
         echo  $row['Last_Name'];
            else
              echo $row['Common_Name'];
?>

<script type="text/javascript">
    $(document).ready(function(){
        _name = "<?php echo $row['Common_Name'] ?>";

        if (_name.length > 14) {
            $('body').css('font-size', '12px');
        };
    });
</script>
Viktor Maksimov
  • 1,465
  • 1
  • 10
  • 11
  • 2
    Answers should not require the use of a library that is neither tagged nor requested in the question. – RobG Nov 04 '15 at 22:43
  • 2
    You shouldn't be embedding font sizes in server side code. Use a class and set the size in the css. Also, including jquery if you're not already using it to achieve this trivial task is kind of like renting an industrial crane to move a pebble. – blm Nov 04 '15 at 22:44