Millhorn
Millhorn

Reputation: 3166

Apply CSS Styling to PHP output

The following displays HTML results from the database field "Never". I am trying to apply CSS styling to the output.

Here's what I have...

echo "<p><strong>Never:</strong>&nbsp;".$results['Never']."".$results['text']."</p><br />";

Here's what I've tried...

echo "<p><strong>Never:</strong>&nbsp;".$results['<div id="nevermsg">'Never'</div>]."".$results['text']."</p><br />";

Here's my CSS...

#nevermsg { color: red; }

...but it's not applying properly. I am receiving a syntax error and a headache. Am I putting this in the wrong spot?

The $results variable is not being filled.

Edit: Code Added

Here's my connection...

<?php
    mysql_connect("localhost", "jpcso_compliance", "abc*123") or die("Error connecting to database: ".mysql_error());
    /*
        localhost - it's location of the mysql server
        root - username
        third is your password

        if connection fails it will stop loading the page and display an error
    */

    mysql_select_db("jpcsolut_compliance") or die(mysql_error());
    /* jpcsolut_webfro_HS is the name of database we've created */
?>

There is no other HTML formatting for the output, other than what is right here...

<div id="title">
<p><h1>Database Search Results</h1></p></div>
<br />
<div id="main_inner">


<?php
    $query = $_GET['query']; 
    // gets value sent over search form

    $min_length = 2;
    // you can set minimum length of the query if you want

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query); 
        // changes characters used in html to their equivalents, for example: < to &gt;

        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection

        $raw_results = mysql_query("SELECT * FROM Compliance
            WHERE (`Question` LIKE '%".$query."%') OR (`Sample Response / Must` LIKE '%".$query."%') OR (`Must` LIKE '%".$query."%') OR (`Can` LIKE '%".$query."%') OR (`Never` LIKE '%".$query."%') OR (`Tags` LIKE '%".$query."%')") or die(mysql_error());

        // * means that it selects all fields, you can also write: `id`, `title`, `text`
        // IllegitimateHighSchools is the name of our table

        // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
        // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
        // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

        if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

            while($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

                echo "<p><strong><u><h2>Question:</u>&nbsp;".$results['Question']."".$results['text']."</h2></u></strong></p><br />";

                echo "<p><strong>Sample Response / Must:</strong>&nbsp;".$results['Sample Response / Must']."".$results['text']."</p><br />";
                //echo "<p><strong>Location:</strong>&nbsp;<a href='".$results['Location']."' target='_blank'>".$results['SchoolLocation']."</a>".$results['text']."</p><br />";

                echo "<p><strong>Must:</strong>&nbsp;".$results['Must']."".$results['text']."</p><br />";
                echo "<p><strong>Can:</strong>&nbsp;".$results['Can']."".$results['text']."</p><br />";
                //echo "<p><strong>Never:</strong>&nbsp;".$results['Never']."".$results['text']."</p><br />";
                echo  "<span id=\"nevermsg\"><p><strong>Never:</strong>&nbsp;".$results['Never']."".$results['text']."</p></span><br />";
                echo "<p><strong>_____________________________________________</strong>&nbsp;"."</p><br />";                
                echo "<p><strong>Tags:</strong>&nbsp;".$results['Tags']."".$results['text']."</p>";
                // posts results gotten from database(title and text) you can also show id ($results['id'])
            }

        }
        else{ // if there is no matching rows do following

    echo "<br /><br /><br /><strong><h2>"."You have searched a term that is not in the database. Please contact <a href=\"mailto:" . htmlentities('[email protected]') . "\">".htmlentities('[email protected]') . "</a>, if you think this term should be added."."</h2></strong>";
        }

    }
    else{ // if query length is less than minimum
        echo "Minimum length is ".$min_length;
    }
?>
<br />
    <br />
        <br />
            <br />
<br />
    <br />




</div>
<!--End of Inner Main-->

Additionally, here is a link to the site, where I've included a query in the URL here.

Lastly, I call the stylesheet 'global.css', which is where the style lives.

#nevermsg { color: red; }

Upvotes: 0

Views: 2487

Answers (4)

Iron3eagle
Iron3eagle

Reputation: 1077

You need to change the array type in your while loop. mysql_fetch_array will return a standard array accessed like $array[0] not $array['my_key'] so use mysql_fetch_assoc.

So instead of this:

    while ($results = mysql_fetch_array($raw_results)) {
            echo "<p><strong>Never:</strong>&nbsp;<span id=\"nevermsg\">".$results['Never']."</span></p>"; //Doesn't
    }

Do this:

    while ($results = mysql_fetch_assoc($raw_results)) {
            echo "<p><strong>Never:</strong>&nbsp;<span id=\"nevermsg\">".$results['Never']."</span></p>"; //Works
    } 

UPDATE:

Another option if you don't know the key is loop through the $results array itself like so with a foreach:

    while ($results = mysql_fetch_assoc($raw_results)) {
         foreach ($results as $key => $value) {
              echo "<span id=\"nevermsg\"><p><strong>$key:</strong>&nbsp;".$value."</p></span><br/>";
         }
    } 

See the PHP fiddle example of the loop and <span> in action here. For obvious reasons the SQL could not be duplicated in the fiddle.

Upvotes: 2

federicot
federicot

Reputation: 12341

Your query is fine, it's your HTML which is quite messy. Try replacing this:

echo "<p><strong>Never:</strong>&nbsp;".$results['Never']."".$results['text']."</p><br />";

With:

?>
<strong>Never:</strong>
<div id="nevermsg"><?= $results['Never']; ?></div>
<br>
<?php

Upvotes: 0

ATang
ATang

Reputation: 53

Try this:

<?php
echo "<p><strong>Never:</strong>&nbsp;" . $results['<div id=\"nevermsg\">\'Never\'</div>'] . $results['text'] . "</p><br />";
?>

Upvotes: 0

Ilya Streltsyn
Ilya Streltsyn

Reputation: 13536

Why don't you use direct output of HTML?

// some preceding PHP code
?>
<p>
  <strong>Never:</strong>&nbsp;<span id="nevermsg"><?=$results['Never'].$results['text'] ?></span>
</p>

It will be more readable and there will be no mess of HTML and PHP quotes, and no need to escape them...

Upvotes: 0

Related Questions