...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
June 17, 2010

Check variable if explode-able in PHP

Question by ZaneDeFazio

Not sure if there is a way to check a variable if it is explode-able or not…

I have a database of city names some are one word cities and some are multiple word cities

EX: Chicago, Los Angeles

I keep getting an error when use “implode” when a city name is one word, so I tried using “count” and using an if statement… not having any luck

$citi = explode(' ', $row['city']);
$count = count($citi);
if ($count > 1) {
   $city = implode('+', $citi);
}
else {
   $city = $citi;
}

Answer by Ben Rowe

if(strpos($row['city'], ' ') !== false) {
  // explodable
} else {
  // not explodable
}

Answer by Starx

use explode itself to see if it is explodable

$a = explode(" ","Where Am I?");
if(count($a)>1) {
     echo "explodable";
}
else {
     echo "No use of exploding";
}
Read more

expand floating object when floating object within expands

Question by Scarface

Hey guys, quick question, I have a link when clicked drops down a list. This list is floated to the right to position it properly. This list is in another box that has been floated. My problem is that when the list expands, the box does not and the list comes out of the container box, unless the list is not floated. However floating it seems like the only way to get it to the position I want. If anyone has any ideas on how to solve this problem I would appreciate it.

.container-box {
margin-top:0px;
float:left;
padding-left:5px;
position:relative;
}

#box-within {
float:right;
font-weight:bold;
max-height:250px;
display: none;
background-color:#fff;
overflow: auto;
width:325px;
padding:5px;
position:relative;
}

Answer by Ken Redler

This is a clearing problem. One solution is to add another HTML element after your “inner” box: something like <div style="clear: both;"> or equivalent. Another more elegant solution involves setting width and/or height and overflow.

Quirksmode has a good explanation of both methods.

Answer by Starx

OK, here is your solved CSS. I made a slight change inorder to make this noticeable

.container-box {
margin-top:0px;
float:left;
padding:5px;
position:relative;
border:1px #666666 solid;
}

#box-within {
float:right;
font-weight:bold;
width:325px;
padding:5px;
background:#CCCCCC;
}
Read more
June 16, 2010

Should all presentational images be defined in CSS?

Question by Grant Crofton

I’ve been learning (X)HTML & CSS recently, and one of the main principles is that HTML is for structure and CSS for presentation.

With that in mind, it seems to me that a fair number of images on most sites are just for presentation and as such should be in the CSS (with a div or span to hold them in the HTML) – for example logos, header images, backgrounds.

However, while the examples in my book put some images in CSS, they are still often in the HTML. (I’m just talking about ‘presentational’ images, not ‘structural’ ones which are a key part of the content, for example photos in a photo site).

Should all such images be in CSS? Or are there technical or logical reasons to keep them in the HTML?

Thanks,
Grant

Answer by redben

If an image is “content” say in a newspaper article, the editorial image, then use img tag. If it is part of your UI, theme or skin or whatever the name is, then yes put it CSS.

Suggested readings

  • Designing with Web Standards (Zeldman)
  • Bullet Proof Web Design (Dan Cederholm)
  • CSS Mastery (Andy Clark, Andy Budd, Cameron Moll)

Answer by Starx

Sometimes, loading UI images using CSS, also prevents the users from downloading your UI images to their drives, while saving a page.

But of course there are other ways to save them, but just a point to add.

And browsers tend to prioritize CSS more than HTML, so loading images through CSS might be a little faster compared to HTML.

Read more
June 15, 2010

How to import php with javascript?

Question by dcp3450

I know JavaScript is client side and PHP is server-side. I also know this is an odd question. However, the CMS editor I’m using will not allow php to be stored to the database (it’s the same editor used by Dupral). I can store JavaScript though.

Can I import a php file with JavaScript. Or rather read the php file, store the content in a variable then out put the content to the screen?

Answer by zaphod

short answer: no.
long answer: probably not.

reason: see first sentence of your question

Answer by Starx

Even though you manage to store PHP codes inside javascripts like document.write("<? echo $myvar; ?>"); the portion <? echo $myvar; ?> will simply display as text because only server can parse this type of codes.

However, through AJAX, you can execute the PHP code through Javascript and import the results to display to the user.

Read more

What is PHP as "Personal Home Page" and "PHP Hypertext Preprocessor"?

Question by Starx

What is PHP as “Personal Home Page” and “PHP Hypertext Preprocessor”?

Answer by Sarfraz

It is:

PHP: Hypertext Preprocessor

Previously it was:

Personal Home Page

See:

The History of PHP

PHP was written in the C programming
language by Rasmus Lerdorf in 1994 for
use in monitoring his online resume
and related personal information. For
this reason, PHP originally stood for
Personal Home Page“. Lerdorf combined
PHP with his own Form Interpreter,
releasing the combination publicly as
PHP/FI (generally referred to as PHP
2.0) on June 8, 1995. Two programmers, Zeev Suraski and Andi Gutmans, rebuilt
PHP’s core, releasing the updated
result as PHP/FI 2 in 1997. The
acronym was formally changed to PHP:
HyperText Preprocessor
, at this time.
(This is an example of a recursive
acronym: where the acronym itself is
in its own definition.)

Answer by Starx

Personal Home Page was full form of PHP initially.

Nowadays, PHP stands for PHP Hypertext preprocessor. Still you can word see PHP in this elaboration which still is Personal Home Page

Read more

Monitoring for postbacks with PHP?

Question by Rebecca

I have a PHP page with content that my users can view. When this page receives a POST request from a certain external URL, I’d like to redirect the user to another page.

The problems I’m’ having are:

How can I monitor the page for requests being sent in an efficient way?

How can I actually redirect them since header() doesn’t work.

Thanks.

Answer by Starx

we can put this snippet to check the post request

if($_POST['flag']==1) {
          header("location:newpage.php");
          exit();
}

But If you want to check the request regularly without user interaction than you will have to use AJAX

Read more
June 14, 2010

How to make categories and current page highlighting in PHP?

Question by Markus Ossi

I am trying to find some example code or best practices about making CMS-type categories with PHP.

This is a problem that has for sure been solved gazillion times but for some reason I am unable to find any example code using PHP about how to implement this.

As far as I can tell, there are two parts to the problem. The first one has to with the styling side of things:

  • outputting the link in the navigation so that the current page has a special style (class=”active”) and
  • to not print out the link for the current page.

The second part is handling categories, subcategories and the dynamic pages under the categories.

The second part seems pretty straightforward. I am thinking of making it so that the name of the category in the navigation is a link to categories.php?id=x and on this page I just print out the pages with that category id. Then, if the user klicks on a page he will be taken to pages.php?id=y.

However, I am not quite sure on how to make a navigation to check if we are now on the current page. Should I just use some case switch or what?

Any ideas or links to some good example code are much appreciated.

Answer by epalla

If you’re loading the page CMS-style presumably you’ve got some sort of page identifier that’s accessible within the code? A current_page_id or something like that? From there I usually just do this:

<ul id="menu"><?
foreach ($menu_items as $menu_item) {
    ?><li <?=($menu_item['page_id'] == $current_page_id) ? 'class="active"' : ''?>><a href="<?=$menu_item['link']?>"><?=$menu_item['title']?></a></li><?
}
?></ul>

Answer by Starx

no need for PHP, use CSS

a:active { color:#09f; }

UPDATE
use this little jquery code

$("*").find("a[href='"+window.location.href+"']").each(function(){
   $(this).addClass("current");
   $(this).attr('href',"#"); //nullifying the link
   //add your own logic here if needed
})
Read more

Several buttons on one form calling same function

Question by user198003

trying to develop web form using jquery.

all i need is to have several (don’t know how many) buttons on one form.

all of those buttons have to call one same function, and to pass one parameter to that function. that function have to do some post method, but i can handle it.

so, my main problem is that i don’t know how to develop JS that will call specific jquery function.

can you help me with this?

Answer by Starx

Or, you can give a common class to all the links and do something like this

A sample HTML

<a class="yourclass" param1="value1" href=#">Text</a>

Now the script

$(".yourclass").click(function() {

var param = $(this).attr('param1');
//now do the remaing

});
Read more

Fix position of image

Question by andrei

I have this list
http://pastebin.me/dde64f8c185de9dd5e429f84701a01ce
Anytime you click on an image extra content appears . I have tryed several css methods but i cant get the images to remain in their position and get the text to go underneath . Anyone has a solution ?

Answer by Starx

Ok, Solved Here is your html

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

     <title>Test Page</title>

     <style type="text/css">
        * { margin: 0; padding: 0;}

        .holder { width:105px;float:left; }

                img {   width: 105px;
            float:left;
                    }

        .content {
            display:none;
            float:left;
            }

                #container { width:350px;margin:auto; }
     </style>

    <script type="text/javascript" src="http://www.feetjeans.com/new_fj/nou/jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){

        $('img').click(function(){
            var clasa = $(this).attr("class");
            var divul = '#' + clasa;
            var cssul = $(divul).css('display');
            if(cssul == 'none') { $(divul).fadeIn(500); }
            else $(divul).hide(500);
                        console.log('test');
        });

});

    </script>

</head>

<body>
    <div id="container">
        <div class="holder">                    
                    <img src="http://img21.imageshack.us/img21/1706/vansf.jpg" class="0" align="left" />
                    <span class="content" id="0">This is content</span>
                </div>
        <div class="holder">                    
                    <img src="http://img21.imageshack.us/img21/1706/vansf.jpg" class="1" />
                    <span class="content" id="1">This is content</span>
                </div>
    </div>
</body>

</html>

UPDATE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

     <title>Test Page</title>

     <style type="text/css">
        * { margin: 0; padding: 0;}

        .holder { width:350px;float:left; }

                img {   width: 105px;
            float:left;
                    }

        .content {
            display:none;
            border: 1px #09f solid;
            }

        #container { width:350px;margin:auto; }
     </style>

    <script type="text/javascript" src="http://www.feetjeans.com/new_fj/nou/jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){

        $('img').click(function(){
            $(".content").hide("fast");
            var clasa = $(this).attr("class");
            var divul = '#' + clasa;
            var cssul = $(divul).css('display');
            if(cssul == 'none') { $(divul).fadeIn(500); }
            else $(divul).hide(500);
                        console.log('test');
        });

});

    </script>

</head>

<body>
    <div id="container">
        <div class="holder">                    
                    <img src="http://img21.imageshack.us/img21/1706/vansf.jpg" class="0" />
                    <img src="http://img21.imageshack.us/img21/1706/vansf.jpg" class="1" />
                    <img src="http://img21.imageshack.us/img21/1706/vansf.jpg" class="2" />
                    <div style="clear:both;"></div>
                    <div class="content" id="0">This is content</div>
                    <div class="content" id="1">This is content</div>
                    <div class="content" id="2">This is content</div>
        </div>
        <div class="holder">                    
                    <img src="http://img21.imageshack.us/img21/1706/vansf.jpg" class="3" />
                    <img src="http://img21.imageshack.us/img21/1706/vansf.jpg" class="4" />
                    <img src="http://img21.imageshack.us/img21/1706/vansf.jpg" class="5" />
                    <div style="clear:both;"></div>
                    <div class="content" id="3">This is content</div>
                    <div class="content" id="4">This is content</div>
                    <div class="content" id="5">This is content</div>
        </div>
    </div>
</body>

</html>
Read more
...

Please fill the form - I will response as fast as I can!