How to style a PHP generated label? - php

I have this IF clause snippet from a PHP script that is basically a small search engine:
if(isset($_REQUEST['search'])){
$search_term = trim($_REQUEST['search']);
if(strlen($search_term) <= 0){
$website_search_dynamic -> error($website_search_dynamic->label("Please enter a search query."),true);
}else if(strlen($search_term) < $GLOBALS['_SEARCH_MIN_CHARS']){
$website_search_dynamic -> error($website_search_dynamic->label("Sorry, you must enter at least %d characters in your search query",$GLOBALS['_SEARCH_MIN_CHARS']),true);
}
$website_search_dynamic -> search($search_term);
}
How do I add CSS styling to the two labels? For example, I would like either error message to be displayed in a centered div. Nothing too fancy.
Thanks!

You could just do:
if(strlen($search_term) <= 0){
$error[] = "Nope";
}
And then wherever you want in your html, put:
<?php
if(!empty($error)) {
echo "<div align='center' class='foo'>" . $error [0] . "</div>";
}
?>
And style the class however you want.

You could even try this method
$website_search_dynamic -> error($website_search_dynamic->label("<span style="text-align:center;color:red">Please enter a search query.</span>"),true);
php can run html codes you can simple add html inline style code to work
try google and find how to add external style to php file if you wish(because i don't know external style is possible in php

Related

Add html with css to div without editing main css

I have a message in my solution.
The problem is when I want to add a mail (html + css), the main css is changing.
This is a test concluded:
And this is an bad example (other mail):
I am putting an e-mail html like this:
<div class="inbox-view">
<?php
function isHTML($string) {
if ($string != strip_tags($string)) {
return true;
} else {
return false;
}
}
if (isHTML($mail[0]->BODY)) {
echo $mail[0]->BODY;
} else {
echo str_replace("\n", "<br/>", $mail[0]->BODY);
}
?>
</div>
I think you can consider using shadow DOM here as it will separate the css from other code on the page so that different parts do not clash. But i am afraid that you can do it using javscript only.
You can find the details on how to create shadow DOM from here.
https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM
How about using more specific selectors.
e.g. div#first p#myParagraph span.bolder
Or use !important as last resort.

Displaying other document extensions in a PHP/mysql database

OK this is my first post here so, hello! and please go easy on me if this is a simple solution.
I am creating a document database using apache2,mysql and php. I have only had previous experience with html,css and a small amount of javascript so this has been a steep learning curve and a good challenge for me.
I have successfully created the database and uploaded documents into it. i can search and link to the documentment required in a html/php screen but i only seem to be able to use/search, recall one extension type. this would be either .PHP, .PDF, .html, or .docx etc. In reality not all documents are or will be PDF's, below is my script:
/* -- end of connection script --*/
if(isset($_GET['search'])) {
$get_value = $_GET['user_query'];
if($get_value=='') {
echo "<center id='alert'><b>nothing in the search field</b></center>";
exit();
}
if($get_value=='Search...'){
header('Location: index.php');
}
$result_query = "SELECT * from search WHERE site_keywords LIKE '%$get_value%' OR site_title LIKE '%$get_value%' OR site_description LIKE '%$get_value%' ";
$run_result = mysql_query($result_query);
?>
<h3 class="total"><?php echo $row_result; ?>Results for '<?php echo $get_value; ?>'</h3>
<?php
if(mysql_num_rows($run_result)<1) {
echo "<center id='alert'><b>Oops! no search results found</b></center>";
exit();
}
while($row_result=mysql_fetch_array($run_result)) {
$site_id=$row_result['site_id'];
$site_title=$row_result['site_title'];
$site_link=$row_result['site_link'];
$site_description=$row_result['site_description'];
$site_file=$row_result['site_file'];
echo"<div class='results'>
<h3><a href='$site_id.php' target='_blank'>$site_title</a></h3>
<p align='justify'>$site_description</p>
</div>";
}
}
?>
as you can see from the echo results the href file requires an extension (in this case .php) to correctly display a .php document but not other extension types. If i change this to .pdf it will display the .pdf but not .php and so on. I am at a loss.

Changing and retaining specific a-tag background color once clicked in PHP

I've got a a-tag dynamically populated which extracts data from a database and multiplies to the number of records existing. So this to be looked at more like an item in a list.
echo "<a class='Keylist ".$wecb_color."' id='myIDhere' href='index.php?idd=".$record['categoryid']."'><div>".$record['descriptor']."</div></a>";
I'm trying to keep one of the a-tags background set on a different color when a user clicks. Behavior I'm trying to achieve is when a user scrolls and clicked on one link, that a-tag to remain selected depicting a different background color.
I've written snippet trying to do that.
$wecb_color ='';
if(isset($_GET['idd'])){
$_SESSION['link'] = $_GET['idd'];
echo "<br><br><br><br><br>".$_SESSION['link'];
if($_GET['idd'] == $_SESSION ['link']) {
$wecb_color = 'changetogreen';
}
}
The issue is this makes all the a-tag's which are generated having a background color changed to green instead the one that user clicks.
Hard coded CSS script reads as :- .changetogreen{background-color: green;}
Any thoughts appreciated.
It'd change the background color of all links because you're changing the whole class style.
Try this:
echo "<a class='"Keylist " style=" '.$wecb_style.' ;" id='myIDhere' href='index.php?idd=".$record['categoryid']."'><div>".$record['descriptor']."</div></a>";
So you can change the style of particular tag.
And PHP would be:
$wecb_style ='';
if(isset($_GET['idd'])){
$_SESSION['link'] = $_GET['idd'];
echo "<br><br><br><br><br>".$_SESSION['link'];
if($_GET['idd'] == $_SESSION ['link']) {
$wecb_style = 'background: green';
}
}

How can I check the source of all image tags in user input using jQuery

I have a form for user input in which users can add images hosted elsewhere using a form. The images are displayed as a small icon which is both a link and which will load an image into a div with stacking order +1 on hover. The image source address is stored in the link tag only.
I am using a <div /> with contenteditable=true for the user input. The icon is appended when the form is used. The code for this part works fine. What I would like to do is check the source of all image tags to make sure that users are not adding their own html to display full size images in their post.
I am using php on the backend to remove all tags except links and images, but would like to use jQuery to check the src of the image tags before posting.
<img src="my_icon" /> //this is what my form will input
<img src="anything_else"> //this is what I want to prevent
Update: I apologize if this is not clear. Essentially, I don't want the user to be able to input any html of their own. If they want to add an image, they have to use my built in form which inserts something like above.
You could loop over the images and then check the src attribute.
$("img").each(function(index) {
if ($(this).attr("src") == ...) {
// do something
}
}
See http://api.jquery.com/each/ and http://api.jquery.com/attr/ for more information.
Say we have the <div id="editor" /> the jQuery script would look something like this:
var srcs = [];
jQuery ('div#editor img').each (function () {
srcs.push (jQuery (this).attr ('src'));
});
srcs will now hold all the src-attributes from the <img />-tags provided in the <div id="editor" /> tag.
Especially for your site following code alerts the links for all images:
$('.postimage').each(function(){
alert($(this).attr('href'));
});
I have an answer. When users input a < or > in a contenteditable="true" div, the browser replaces them with the html notation < and >. The good news is the images would have never displayed when outputting the user comment. The bad news is that the jQuery based solutions given above will not work to remove the ugly coding. I ended up using php to do it with
$post = $_POST['comment'];
$imgcheck = true;
$stringstart = 0;
while($imgcheck == 'true'){
if($stringstart = strpos($post,'<img',$stringstart)){
if ($stringend = strpos($post,'>',$stringstart)){
$strlength = $stringend - $stringstart +4;
$substring = substr($post,$stringstart,$strlength);
if (!preg_match('~src="\/images\/ImageLink.jpg"~',$substring)){
$post = str_replace($substring, "", $post);
}
else{
$stringstart = $stringend;
}
}
else{
$imgcheck = 'false';
}
}
else{
$imgcheck = 'false';
}
}

How do I highlight text in PHP with conditions?

I have some numbers, and depending on the number's value I need to highlight a string which is in a table. The table is not in database.
Does anyone know how to highlight text in php?
PHP won't "highlight" text, that is done with CSS/HTML. You only use PHP to output the correct HTML.
Something like this:
$my_num = 9;
foreach ($my_array_of_numbers as $num)
{
// See if $num matches $my_num, if so - add the "highlight"
// class to the HTML element we're using
$css_class = ($num == $my_num) ? 'highlight' : '';
echo '<td class="'.$css_class.'">'.$num.'</td>';
}
Then in your CSS:
.highlight {
background:yellow;
}
You may also want to consider using javascript for this, which can handle the task after the HTML has already been generated.
Presentation, like highlighting, is done in CSS, not PHP.
My advice would be to create a CSS class which has a background-color defined to be used as highlighting. Then, in PHP, you can conditionally wrap your string in <span> tags which have that class, so the text would be highlighted.
CSS:
.highlight {
background-color: #FF0;
}
PHP:
if (highlightCondition) {
echo '<span class="highlight">' . $string . '</span>';
} else {
echo $string;
}

Categories