Lower case - Not Woring

Discussion in 'PHP' started by misohoni, Jun 15, 2014.

  1. #1
    I'd like to change <?=$beard['comments']?>to <?=$comments?> to I can run the lower case, something like this:

     $comments = $row["comments"];
    $comments = mb_convert_case($comments, MB_CASE_LOWER, "UTF-8"); 
    Code (markup):
    But it doesn't work. If I ran strtolower on the $query, doesn't work either

    <?
    $number_of_items = 6;
    $query = "SELECT * FROM abc WHERE hidden = 0 ORDER BY date_submitted DESC LIMIT 0, $number_of_items";
    $result = doQuery($query);
    while ($beard = mysql_fetch_assoc($result))
     
    { ?>
     
    <a href="beard-<?=$beard['drawingID']?>-<?=$beard['comments']?>"><img src="/thumbnails/<?=$beard['drawingID']?>.png" class="thumbnail" alt="<?=$beard['comments']?> Drawing" border="0" style=""></a>
    <? } ?>
    Code (markup):
    Any thoughts? thanks
     
    misohoni, Jun 15, 2014 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Well, first I'd stop using so many opening and closings of PHP, particularly the poorly supported and rarely enabled shorttag <?

    Then I'd stop using mysql_ functions like it's 2004

    THEN I'd stop plugging string values into queries, making extra strings for nothing, etc, etc...

    guessing wildly:
    <?php
    
    $number_of_items = 6;
    
    // assumes $db is a connected PDO object
    
    /*
    	turn off emulated prepares since that screws up LIMIT and native driver
    	is more efficient
    */
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    
    $statement = $db->select('
    	SELECT * FROM abc
    	WHERE hidden = 0
    	ORDER BY date_submitted DESC
    	LIMIT 0, :numItems
    ');
    
    $statement->execute([':numItems' => 6]);
    
    while ($beard = $statement->fetch(PDO::FETCH_ASSOC)) {
    	echo '
    		<a href="beard-', $beard['drawingID'], '-', mb_convert_case(
    			$beard['comments'], MB_CASE_LOWER, 'UTF-8'
    		), '">
    			<img
    				src="/thumbnails/', $beard['drawingID'], '.png"
    				alt="', $beard['comments'], ' Drawing"
    				class="thumbnail"
    			/>
    		</a>';
    }
    
    ?>
    Code (markup):
    Though if I was going to use them for a href, I'd be doing MB_CASE_TITLE and then stripping out whitespace with a regex -- poof, InstantCamelCase... just in case your 'comments' has spaces in it. Likewise I'd probably URLEncode it as well.
     
    Last edited: Jun 16, 2014
    deathshadow, Jun 15, 2014 IP
    misohoni likes this.
  3. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #3
    Thanks and Wow I haven't even heard of most of these terms and pulls up this error:
    Call to a member function setAttribute() on a non-object
     
    misohoni, Jun 16, 2014 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    For that to function $db must be a connected PDO object (hence the comment) -- replacing the mysql_ functions you are currently trying to use.

    If you haven't declared a

    $db = new PDO(
      'mysdl:hostname=localhost;dbname=database',
      'username',
      'password'
    );
    Code (markup):
    Instead of the mysql_connect you are likely currently using (and should have stopped using eight years ago) then the above code won't work. Hence the "stop using mysql_ functions like it's still 2004" part... and hence the giant red warning boxes in the manual that have been there for over two years no warning you of the same!

    Which is why I look out upon the web with digust to the point of nausea at the number of poorly written buggy insecure tutorials still telling people to use mysql_ functions.
     
    deathshadow, Jun 16, 2014 IP
    misohoni likes this.
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    Try having MySQL do that for you:

    
    $query = "SELECT drawingID, LOWER(comments) as comments FROM abc WHERE hidden = 0 ORDER BY date_submitted DESC LIMIT 0, $number_of_items";
    
    Code (markup):
    See if that makes a difference. Another option like @deathshadow mentioned switch to using PDO if that is an easy task to do.

    Also what type of field is comments?
     
    ThePHPMaster, Jun 16, 2014 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    LOWER is nice when it works, but I've found it to be unreliable unless you are 100% sure of your TABLE's encoding. It screws up too much when the stored data doesn't match the table encoding -- something most people never change from the default.
     
    deathshadow, Jun 16, 2014 IP