How to do this in PHP - making an output row bold automatically

Discussion in 'PHP' started by Gerben H., Oct 8, 2012.

  1. #1
    I like to make automatically an output-row bold which includes the word "hompa". How can i do this?
    I have this query:

    $db= mysql_connect( "localhost", "database_soccer", "password" );
    mysql_select_db( "database_soccer", $db );
    $sql = 'SELECT * FROM `database_soccer` WHERE `TeamID` =17';
    $result = mysql_query( $sql, $db );

    echo "<table border=\"1\" align=\"left\" width=\"406\" height=\"402\">";
    echo "<tr><th align=\"left\"><strong>Elftal</strong></th>";
    echo "<th align=\"left\"><strong>Plaats</strong></th>";
    echo "<th align=\"left\"><strong>G</strong></th>";
    echo "<th align=\"left\"><strong>W</strong></th>";
    echo "<th align=\"left\"><strong>GW</strong></th>";
    echo "<th align=\"left\"><strong>V</strong></th>";
    echo "<th align=\"left\"><strong>P</strong></th>";
    echo "<th align=\"left\"><strong>DPV</strong></th>";
    echo "<th align=\"left\"><strong>DPT</strong></th>";
    echo "<th align=\"left\">PM</strong></th></tr>";
    while( $row = mysql_fetch_array( $result )) {
    echo "<tr><td>";
    echo $row['Elftal'];
    echo "</td><td>";
    echo $row['Plaats'];
    echo "</td><td>";
    echo $row['G'];
    echo "</td><td>";
    echo $row['W'];
    echo "</td><td>";
    echo $row['GW'];
    echo "</td><td>";
    echo $row['V'];
    echo "</td><td>";
    echo $row['P'];
    echo "</td><td>";
    echo $row['DPV'];
    echo "</td><td>";
    echo $row['DPT'];
    echo "</td><td>";
    echo $row['PM'];
    echo "</td></tr>";
    }

    echo "</table>";

    ?>
     
    Gerben H., Oct 8, 2012 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    1) You're using TH, what's with the non-semantic use of STRONG?

    2) If they're all receiving the same ALIGN, NONE of them should be getting ALIGN -- set that once in your stylesheet not on every blasted element.

    3) since those are column headings, you should be using the SCOPE attribute, AND that entire first TR should probably be in a THEAD.

    4) in MODERN coding practice there is no such thing as BORDER or ALIGN on table, and it's a REALLY bad idea to fix either the width OR the height!

    5) since this appears to be new code, why are you using the outdated and unsafe/insecure mysql_ functions?

    6) ease up on echo -- you're using 20+ of them to do the job of one!

    7) Single quotes would be faster AND mean you'd not need to escape your doubles.

    8) what field would contain "hompa"?!? Any of them, just one of them?

    Assuming just the first field needs to be tested:
    
    echo '
    	<table class="soccerResults">
    		<thead>
    			<tr>
    				<th scope="col"></strong></th>
    				<th scope="col">Plaats</th>
    				<th scope="col">
    				<th scope="col">W</th>
    				<th scope="col">GW</th>
    				<th scope="col">V</th>
    				<th scope="col">P</th>
    				<th scope="col">DPV</th>
    				<th scope="col">DPT</th>
    				<th scope="col">PM</th>
    			</tr>
    		</thead><tbody>';
    		
    while ($row=mysql_fetch_array($result)) {
    	echo '
    			<tr',(
    				stripos($row['Elftal'],'hompa')===false ? '' : ' class="hasHompa"'
    			),'>
    				<td>',$row['Elftal'],'</td>
    				<td>',$row['Plaats'],'</td>
    				<td>',$row['G'],'</td>
    				<td>',$row['W'],'</td>
    				<td>',$row['GW'],'</td>
    				<td>',$row['V'],'</td>
    				<td>',$row['P'],'</td>
    				<td>',$row['DPV'],'</td>
    				<td>',$row['DPT'],'</td>
    				<td>',$row['PM'],'</td>
    			</tr>';
    }
    
    echo '
    		</tbody>
    	</table>';
    
    ?>
    
    Code (markup):
    and the CSS for that:
    
    .soccerResults {
    	width:406px; // this is probably a bad idea
    	/*
    		and NEVER state a height on content text --  at that point you might
    		as well piss on accessibility and declare all your content in px 
    		metric fonts
    	*/
    	border:0;
    }
    
    .soccerResults thead th {
    	text-align:left;
    	/*
    		default behavior of th is bold, so no need to say bold
    		or waste time/bandwidth on STRONG tags!
    	*/
    }
    
    .soccerResults .hompa td {
    	font-weight:bold;
    }
    
    Code (markup):
     
    deathshadow, Oct 8, 2012 IP
  3. Gerben H.

    Gerben H. Well-Known Member

    Messages:
    293
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    143
    #3
    I am so sorry I am not an coder. I tried your script but it gives me an error. I am afraid i cant fix this.
     
    Gerben H., Oct 8, 2012 IP
  4. thepresence

    thepresence Greenhorn

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #4
    Gerben,

    Without much coding, you can accomplish this.

    First, make your "row return" equal to a variable like this:

    $plaats= echo $row['Plaats'];

    Then, do use this code:

    <?php
    $pos = strpos($haystack,$needle);
    // example: $pos = strpos($plaats,'hompa');

    if($pos === false) {
    // string needle NOT found in haystack
    // so, make the table row as you have it above
    }
    else {
    // string needle found in haystack
    // so, make your table row bold with the <b> command
    }
    ?>



    It looks like this is what you were trying to accomplish.

    If so, hope this helps. Cheers,
    TP
     
    thepresence, Oct 8, 2012 IP