Xtremer360 Posted May 4, 2011 Share Posted May 4, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/ Share on other sites More sharing options...
harristweed Posted May 4, 2011 Share Posted May 4, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210280 Share on other sites More sharing options...
Xtremer360 Posted May 4, 2011 Author Share Posted May 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210282 Share on other sites More sharing options...
kney Posted May 4, 2011 Share Posted May 4, 2011 it means that the offset is longer than the string ur having Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210283 Share on other sites More sharing options...
Xtremer360 Posted May 4, 2011 Author Share Posted May 4, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210284 Share on other sites More sharing options...
kney Posted May 4, 2011 Share Posted May 4, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210285 Share on other sites More sharing options...
Xtremer360 Posted May 4, 2011 Author Share Posted May 4, 2011 and btw, where does the $row['question'] come from? Did you even read my first post? Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210289 Share on other sites More sharing options...
kney Posted May 4, 2011 Share Posted May 4, 2011 I did but can you paste ur complete code from that page? Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210299 Share on other sites More sharing options...
cyberRobot Posted May 4, 2011 Share Posted May 4, 2011 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210334 Share on other sites More sharing options...
Xtremer360 Posted May 4, 2011 Author Share Posted May 4, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210422 Share on other sites More sharing options...
cyberRobot Posted May 4, 2011 Share Posted May 4, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210425 Share on other sites More sharing options...
Xtremer360 Posted May 4, 2011 Author Share Posted May 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210428 Share on other sites More sharing options...
Adam Posted May 4, 2011 Share Posted May 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210431 Share on other sites More sharing options...
cyberRobot Posted May 4, 2011 Share Posted May 4, 2011 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 { ... } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210433 Share on other sites More sharing options...
Xtremer360 Posted May 4, 2011 Author Share Posted May 4, 2011 Its only flashing it for a second then taking it off and keeping only the string. Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210436 Share on other sites More sharing options...
cyberRobot Posted May 4, 2011 Share Posted May 4, 2011 That's weird, do you have a redirect set up or something? Or maybe you have some CSS that causes the image to move over top of the string? Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210438 Share on other sites More sharing options...
Xtremer360 Posted May 4, 2011 Author Share Posted May 4, 2011 I don't that's what so weird. I sent you a PM. Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210439 Share on other sites More sharing options...
cyberRobot Posted May 4, 2011 Share Posted May 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210507 Share on other sites More sharing options...
Xtremer360 Posted May 4, 2011 Author Share Posted May 4, 2011 Thank you again for all your help and time. Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210511 Share on other sites More sharing options...
cyberRobot Posted May 4, 2011 Share Posted May 4, 2011 Thank you again for all your help and time. No problem, glad it finally works! Quote Link to comment https://forums.phpfreaks.com/topic/235485-string-count/#findComment-1210517 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.