mysql - Apply CSS Styling to PHP output -
the following displays html results database field "never". trying apply css styling output.
here's have...
echo "<p><strong>never:</strong> ".$results['never']."".$results['text']."</p><br />";
here's i've tried...
echo "<p><strong>never:</strong> ".$results['<div id="nevermsg">'never'</div>]."".$results['text']."</p><br />";
here's css...
#nevermsg { color: red; }
...but it's not applying properly. receiving syntax error , headache. putting in wrong spot?
the $results
variable not being filled.
edit: code added
here's connection...
<?php mysql_connect("localhost", "jpcso_compliance", "abc*123") or die("error connecting database: ".mysql_error()); /* localhost - it's location of mysql server root - username third password if connection fails stop loading page , display error */ mysql_select_db("jpcsolut_compliance") or die(mysql_error()); /* jpcsolut_webfro_hs name of database we've created */ ?>
there no other html formatting output, other 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 on search form $min_length = 2; // can set minimum length of query if want if(strlen($query) >= $min_length){ // if query length more or equal minimum length $query = htmlspecialchars($query); // changes characters used in html equivalents, example: < > $query = mysql_real_escape_string($query); // makes sure nobody uses sql injection $raw_results = mysql_query("select * compliance (`question` '%".$query."%') or (`sample response / must` '%".$query."%') or (`must` '%".$query."%') or (`can` '%".$query."%') or (`never` '%".$query."%') or (`tags` '%".$query."%')") or die(mysql_error()); // * means selects fields, can write: `id`, `title`, `text` // illegitimatehighschools name of our table // '%$query%' we're looking for, % means anything, example if $query hello // match "hello", "hello man", "gogohello", if want exact match use `title`='$query' // or if want match full word "gogohello" out use '% $query %' ...or ... '$query %' ... or ... '% $query' if(mysql_num_rows($raw_results) > 0){ // if 1 or more rows returned following while($results = mysql_fetch_array($raw_results)){ // $results = mysql_fetch_array($raw_results) puts data database array, while it's valid loop echo "<p><strong><u><h2>question:</u> ".$results['question']."".$results['text']."</h2></u></strong></p><br />"; echo "<p><strong>sample response / must:</strong> ".$results['sample response / must']."".$results['text']."</p><br />"; //echo "<p><strong>location:</strong> <a href='".$results['location']."' target='_blank'>".$results['schoollocation']."</a>".$results['text']."</p><br />"; echo "<p><strong>must:</strong> ".$results['must']."".$results['text']."</p><br />"; echo "<p><strong>can:</strong> ".$results['can']."".$results['text']."</p><br />"; //echo "<p><strong>never:</strong> ".$results['never']."".$results['text']."</p><br />"; echo "<span id=\"nevermsg\"><p><strong>never:</strong> ".$results['never']."".$results['text']."</p></span><br />"; echo "<p><strong>_____________________________________________</strong> "."</p><br />"; echo "<p><strong>tags:</strong> ".$results['tags']."".$results['text']."</p>"; // posts results gotten database(title , text) can show id ($results['id']) } } else{ // if there no matching rows following echo "<br /><br /><br /><strong><h2>"."you have searched term not in database. please contact <a href=\"mailto:" . htmlentities('email@domain.com') . "\">".htmlentities('email@domain.com') . "</a>, if think term should added."."</h2></strong>"; } } else{ // if query length less minimum echo "minimum length ".$min_length; } ?> <br /> <br /> <br /> <br /> <br /> <br /> </div> <!--end of inner main-->
additionally, here link site, i've included query in url here.
lastly, call stylesheet 'global.css', style lives.
#nevermsg { color: red; }
you need change array type in while
loop. mysql_fetch_array
return standard array accessed $array[0]
not $array['my_key']
use mysql_fetch_assoc
.
so instead of this:
while ($results = mysql_fetch_array($raw_results)) { echo "<p><strong>never:</strong> <span id=\"nevermsg\">".$results['never']."</span></p>"; //doesn't }
do this:
while ($results = mysql_fetch_assoc($raw_results)) { echo "<p><strong>never:</strong> <span id=\"nevermsg\">".$results['never']."</span></p>"; //works }
update:
another option if don't know key
loop through $results
array foreach
:
while ($results = mysql_fetch_assoc($raw_results)) { foreach ($results $key => $value) { echo "<span id=\"nevermsg\"><p><strong>$key:</strong> ".$value."</p></span><br/>"; } }
see php fiddle example of loop , <span>
in action here. obvious reasons sql not duplicated in fiddle.
Comments
Post a Comment