0

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; }
16
  • 1
    That can't possibly be valid PHP. Parse error: syntax error, unexpected 'Never' (T_STRING), expecting ']' Commented Jul 25, 2013 at 17:49
  • The top one is valid PHP. The bottom one, apparently is not. Commented Jul 25, 2013 at 17:59
  • 1
    @webfrogs Then you need to post the your PHP code that makes the data base connection and the query. My guess is that is where it is failing. Commented Jul 25, 2013 at 18:20
  • 2
    Is your question about the PHP syntax error, or the CSS "make it red" code? If it's about PHP, clarify your question to focus on that, and tag it PHP*. If it's about the CSS, remove all the PHP and provide raw HTML and CSS which demonstrate your problem. Commented Jul 25, 2013 at 18:23
  • 1
    First you index your result array with 'Never' then with an html tagged 'Never'. What are u trying to do? Commented Jul 25, 2013 at 18:24

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

10 Comments

You may want to do a echo print_r($results); just see what the key and value is for each column being returned. It's fairly easy to then tweak code from there if your key isn't right.
I did that. Curious about why you had me echo the question. Either way, it hasn't changed anything with styling the output of the "Never" line. You can re-review the link here
I added your update, but all it did was repeat the results. Now, it's displaying two sets of results. You can view here
@webfrogs that tells me that you don't need that second key out put. I removed it... still doesn't explain the lack of data. Have you done a echo print_r($results);? That will tell you what your actually returning. Do you mind just adding that line and I'll take a look at the output.
The query is fine. I'm returning the results I want. I just want this line echo "<p><strong>Never:</strong>&nbsp;".$results['Never']."".$results['text']."</p><br />"; to return the results in the font color red.
|
0

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...

2 Comments

This isn't his problem as discovered in the comment threads. His query doesn't seem to be working.
The query works just fine. I want the output to display in the color red.
0

Try this:

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

1 Comment

Unfortunately, all it did was hide the display of the output I'm trying to make red.
0

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

1 Comment

I tried that and I got a server error... Page wouldn't even display.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.