Jump to content

String Count


Xtremer360

Recommended Posts

What I was trying to do was take the value of the question from the resultset and get it to only show the first 30 characters then check to see if its in the middle of a word or not and do a while loop until it reaches the first space.  And then have it add the ... after it. It echos the dots but doesn't echo the question string.

 

$query = "SELECT  
    polls.ID, 
    polls.question, 
    polls.totalVotes 
FROM 
    polls 
ORDER BY 
    polls.dateCreated";     

$result = mysqli_query ( $dbc, $query ); // Run The Query
$rows = mysqli_num_rows($result);

$question = stripslashes($row['question']);
$string = substr($string, 0, 30);
$char = strlen($string);
while ($char > 0) { 
if ($string{$char} == " ") break; 
    $char = $char - 1; 
}
$string = substr($string, 0, $char);

 

Then on my table I have this:

 

<tbody>
    		<?php 
            echo $string;
        while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) {
              echo '
              <tr>
    			  <td>"' . $string . '..."</td>
                  <td><a href=# id="answers" title="' . stripslashes($row['question']) . ' Answers">Answers</a></td>
                  <td>' . $row['totalVotes'] . '</td>
                  <td style="text-align:center;"><img src="img/notepad.png" class="edit" rel="' . $row['ID'] . '"/></td>
		  </tr>';
            }
            ?>
    		</tbody>

Link to comment
Share on other sites

I would find the space after the first 30 characters like this:

$question = stripslashes($row['question']);
$offset =strpos ( $question , " " ,30) ;
$string = substr ($question ,0,$offset);

Also in your code I do not see how the question is added to the $string variable!

Link to comment
Share on other sites

So I copied your exact code and replaced my old code and uploaded and now i get:

 

Warning: strpos() [function.strpos]: Offset not contained in string in /home/xtremer/public_html/efedmanager/mods/php/polls.php on line 19

 

Link to comment
Share on other sites

Its still not echoing the string.

 

<tbody>
    		<?php 
        while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) {
              echo '
              <tr>
    			  <td>"' . $string . '..."</td>
                  <td><a href=# class="answers" title="' . stripslashes($row['question']) . ' Answers">Answers</a></td>
                  <td>' . $row['totalVotes'] . '</td>
                  <td style="text-align:center;"><img src="img/notepad.png" class="edit" rel="' . $row['ID'] . '"/></td>
		  </tr>';
            }
            ?>
    		</tbody>

Link to comment
Share on other sites

But this part of ur code will give more than 1 record right?

 

<?php
$result = mysqli_query ( $dbc, $query ); // Run The Query
$rows = mysqli_num_rows($result);

$question = stripslashes($row['question']);
?>

 

and btw, where does the $row['question'] come from?

Link to comment
Share on other sites

So I copied your exact code and replaced my old code and uploaded and now i get:

 

Warning: strpos() [function.strpos]: Offset not contained in string in /home/xtremer/public_html/efedmanager/mods/php/polls.php on line 19

 

 

As kney suggested, the error comes from the initial string being too short. To prevent the error, wrap the code with an if statement which determines if the string is greater than 30 characters. To get the code to work with where you display the strings, it needs to go inside the while loop:

 

<?php 
while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) {
$question = stripslashes($row['question']);
if(strlen($question) > 30) {
	$offset = strpos($question, " ", 30) ;
	$string = substr($question, 0, $offset);
} else {
	$string = $question;
}

echo '
<tr>
<td>"' . $string . '..."</td>
<td><a href=# class="answers" title="' . stripslashes($row['question']) . ' Answers">Answers</a></td>
<td>' . $row['totalVotes'] . '</td>
<td style="text-align:center;"><img src="img/notepad.png" class="edit" rel="' . $row['ID'] . '"/></td>
</tr>';
}
?>

Link to comment
Share on other sites

As kney suggested, the error comes from the initial string being too short. To prevent the error, wrap the code with an if statement which determines if the string is greater than 30 characters. To get the code to work with where you display the strings, it needs to go inside the while loop:

 

<?php 
while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) {
$question = stripslashes($row['question']);
if(strlen($question) > 30) {
	$offset = strpos($question, " ", 30) ;
	$string = substr($question, 0, $offset);
} else {
	$string = $question;
}

echo '
<tr>
<td>"' . $string . '..."</td>
<td><a href=# class="answers" title="' . stripslashes($row['question']) . ' Answers">Answers</a></td>
<td>' . $row['totalVotes'] . '</td>
<td style="text-align:center;"><img src="img/notepad.png" class="edit" rel="' . $row['ID'] . '"/></td>
</tr>';
}
?>

 

So I used that code changed one thing to add the three dots to the string if its more than 30 characters however its only showing three dots if its less than 30 characters.

 

<tbody>
    		<?php 
        while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) {
            	$question = stripslashes($row['question']);
            	if(strlen($question) > 30) {
            		$offset = strpos($question, " ", 30) ;
            		$string = substr($question, 0, $offset);
                    $string = $string ."...";
            	} else {
            		$string = $question;
            	}
              echo '
              <tr>
    			  <td>"' . $string . '"</td>
                  <td><a href=# class="answers" id="' . $row['ID'] . '" title="' . stripslashes($row['question']) . ' Answers">Answers</a></td>
                  <td>' . $row['totalVotes'] . '</td>
                  <td style="text-align:center;"><img src="img/notepad.png" class="edit" rel="' . $row['ID'] . '"/></td>
		  </tr>';
            }
            ?>
    		</tbody>

Link to comment
Share on other sites

Good point, the ellipsis marks aren't needed if the full string is displayed.

 

As for the issue with the ellipsis marks only showing for strings shorter than 30 characters, I'm not sure what you mean. It works fine for me. Is the code in your last post exactly the same as what you're using?

Link to comment
Share on other sites

Yes and I had it echo the strlen function of the $question variable for the first entry and it said it had 32 characters. So I guess it is still in the first part of the if statement but it still only shows the three dots.

Link to comment
Share on other sites

You can do this a lot more gracefully with regex:

 

$long_question = '12345678901234567890123456789012345 1234567890'; // 35 characters before space
$short_question = preg_replace('/^(.{0,30}[^ ]*).*/', '$1', $long_question);

echo $short_question;

 

Personally I'd only add "..." to the end if the length of $short_question is actually smaller than $long_question.

Link to comment
Share on other sites

So what happens if you echo $string in the if statement?

 

<?php
if(strlen($question) > 30) {
     $offset = strpos($question, " ", 30) ;
     $string = substr($question, 0, $offset);
     $string = $string ."...";

     echo string;
} else {
     ...
}
?>

Link to comment
Share on other sites

As mentioned through PM, the issue is caused by the offset calculation. When there isn't a space after the 30th character, $offset is set to a Boolean false value. And if the third argument in substr() is set to false, it returns an empty string.

http://us.php.net/manual/en/function.substr.php

 

 

The following code should fix the issue:

 

<?php

while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) {
     $question = stripslashes($row['question']);
     if(strlen($question) > 30) {
          //FIGURE OUT WHERE THE NEXT SPACE IS
          $offset = strpos($question, " ", 30);
          
          //IF A SPACE WAS FOUND, SHRINK THE STRING BASED ON $offset
          if($offset !== false) {
               $string = substr($question, 0, $offset) . '...';
               
          //ELSE...USE THE ENTIRE STRING
          } else {
               $string = $question;
          }
     } else {
          $string = $question;
     }
     
     echo '
     <tr>
     <td>"' . $string . '"</td>
     <td><a href=# class="answers" title="' . stripslashes($row['question']) . ' Answers">Answers</a></td>
     <td>' . $row['totalVotes'] . '</td>
     <td style="text-align:center;"><img src="img/notepad.png" class="edit" rel="' . $row['ID'] . '"/></td>
     </tr>';
}

?>

 

 

Note that the new if statement uses '!==' with two equals signs. This tells it to match the value and type, see the following pages for more information:

http://php.net/manual/en/function.strpos.php

http://www.php.net/manual/en/language.operators.comparison.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.