Fluoresce Posted August 14, 2014 Share Posted August 14, 2014 How do I present these documentaries like this: 0-9 10 Things You Need to Know About Sleep 20 Animals That Will Kill You A Ant Kingdom Atkins Diet B Battle of the Brains Body Talk I don't even know where to start! I'm not asking anyone to do this for me; I just need a push in the right direction. Any help will be appreciated. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted August 14, 2014 Share Posted August 14, 2014 It kind of depends on how you are accessing them to begin with. If in a database, you can just specify the ORDER. If coming from somewhere else, you can put the values into an array and then sort() them. 1 Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted August 14, 2014 Author Share Posted August 14, 2014 Yes, I can select them from a database and order them by name. But how do I echo them as explained above? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted August 14, 2014 Share Posted August 14, 2014 By printing a heading with the first letter whenever the first letter changes. 1 Quote Link to comment Share on other sites More sharing options...
paddy_fields Posted August 14, 2014 Share Posted August 14, 2014 (edited) You could do something like this? $documentaries = array("A Great Film","A Not So Great Film","Brilliant Film","Chirpy Film"); $currentHeader = ''; foreach($documentaries as $documentary){ $headerCheck = substr($documentary,0,1); if($currentHeader!==$headerCheck){ echo '<h2>'.$headerCheck.'</h2>'; } $currentHeader = $headerCheck; echo $documentary; echo "</br>"; } Edited August 14, 2014 by paddy_fields 1 Quote Link to comment Share on other sites More sharing options...
Solution Fluoresce Posted August 14, 2014 Author Solution Share Posted August 14, 2014 (edited) Okay, I seem to have done it. What do you guys think? Yes, I know I'm an amateur! I only know if/else statements! $con = mysql_connect("", "", "") or handle_mysql_error("Con failed! "); mysql_select_db("") or handle_mysql_error("Select db failed! "); $sql = "SELECT title, link FROM `doc_table` ORDER BY title ASC"; $query = mysql_query($sql, $con) or handle_mysql_error("Query failed! "); echo"<h1>Full List</h1>"; echo "<div id=\"floated-left\">"; $prevLetter = ''; $count = 0; while($row = mysql_fetch_assoc($query)) { $currLetter = substr($row['title'], 0, 1); if(strpbrk($currLetter, '0123456789') == true) { $currLetter = '0-9'; } if($currLetter !== $prevLetter) { if($prevLetter) { if($counter == 14) { echo "</ol>"; echo "</div><!--Close floated-left-->"; echo "<div id=\"floated-right\">"; } else { echo "</ol>"; } } echo "<h3>$currLetter</h3>"; echo "<ol>"; $prevLetter = $currLetter; $count++; } echo "<li><a href=\"{$row['link']}\">{$row['title']}</a></li>"; } echo "</ol>"; echo "</div><!--Close floated-right-->"; echo "<div class=\"clear\"></div><!--Clear floated-left and floated-right-->"; Edited August 14, 2014 by Fluoresce Quote Link to comment Share on other sites More sharing options...
CroNiX Posted August 14, 2014 Share Posted August 14, 2014 Where does $counter come from? It looks like that should be $count? Not a big deal, but you could use is_numeric() here (I think it reads a little better): if(strpbrk($currLetter, '0123456789') == true) { if (is_numeric($currLetter)) { 1 Quote Link to comment Share on other sites More sharing options...
CroNiX Posted August 15, 2014 Share Posted August 15, 2014 Also, it appears that if you have a title that starts with a lower case "a" and another with a capital "A", it will output in 2 separate headers instead of putting them both under "A". I'd change these lines: $currLetter = strtolower(substr($row, 0, 1)); //force to lowercase for the comparison echo "<h3>" . strtoupper($currLetter) . "</h3>"; //output uppercase 1 Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted August 15, 2014 Author Share Posted August 15, 2014 Where does $counter come from? It looks like that should be $count? Not a big deal, but you could use is_numeric() here (I think it reads a little better): if(strpbrk($currLetter, '0123456789') == true) { if (is_numeric($currLetter)) { Good spot regarding $counter! Thanks for is_numeric(). Used it. Thanks also for the strtoupper() advice. Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted August 15, 2014 Author Share Posted August 15, 2014 You could do something like this? $documentaries = array("A Great Film","A Not So Great Film","Brilliant Film","Chirpy Film"); $currentHeader = ''; foreach($documentaries as $documentary){ $headerCheck = substr($documentary,0,1); if($currentHeader!==$headerCheck){ echo '<h2>'.$headerCheck.'</h2>'; } $currentHeader = $headerCheck; echo $documentary; echo "</br>"; } Thanks for trying. Your code was helpful. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 15, 2014 Share Posted August 15, 2014 What about if the title is not based on the English alphabet? <?php //$row['title'] = 'jazz'; $row['title'] = 'джаз'; $currLetter = substr($row['title'], 0, 1); echo $currLetter You might be considered using multibyte string functions in php. You do nothing to prevent your code of sql injections. Read this article up - https://www.owasp.org/index.php/SQL_Injection 1 Quote Link to comment 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.