...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
May 4, 2012

Get part of URL with PHP

Question by user1069269

I have a site and I would need to get one pages URL with PHP. The URL might be something www.mydomain.com/thestringineed/ or it can www.mydomain.com/thestringineed?data=1 or it can be www.mydomain.com/ss/thestringineed

So it’s always the last string but I dont want to get anything after ?

Answer by TecBrat

You will use the parse_url function, then look at the path portion of the return.
like this:

$url='www.mydomain.com/thestringineed?data=1';
$components=parse_url($url);

$mystring= end(explode('/',$components['path']));

Answer by Starx

parse_url() is the function you are looking for. The exact part you want, can be received through PHP_URL_PATH

$url = 'http://php.net/manual/en/function.parse-url.php';
echo parse_url($url, PHP_URL_PATH);
Read more

Zend 1.10 place websites in virtual subdirectories

Question by Julien

I have the following situation:
We have a webapp built with Zend Framework 1.10 that is available under www.domain.com/webapp

On the server filesystem, we really also have the webapp deployed in /srv/www/webapp

Now, for some reasons I can’t detail too much, the project manager has requested, now that the app is finished, that each client recieves his own url litteraly.
So we would have:
www.domain.com/webapp/client1
www.domain.com/webapp/client2

Normally, what start after the webapp/ would be the controllers, actions and so forth from zend.

Therefore the question: is there a quick way in apache to create these virtual subdirectories, as in the example, client1, client2 ?

I guess it should be possible with url rewriting ?

Thanks in advance

Answer by Starx

Rather than creating virtual directories, this can be solved by creating a specific route with Zend_Route. Assuming, controller as User and the action to pass the name would be view, then

$route = new Zend_Controller_Router_Route(
    'webapp/:username',
    array(
        'controller' => 'user',
        'action'     => 'view',
        'username'   => 'defaultuser'
    )
);
Read more

How to achieve cellpadding with divs or likeiwse?

Question by Nick Rosencrantz

I want a layout almost exactly like the table layout but I understand I should use divs instead to achieve this layout with spacing between elements:
enter image description here

My code that does the above is

<div class="fl20">Personen är sökande i:</div>
        <div class="fl450">
        <table border="0" cellpadding="10"><tr><td>
        <input type="radio" name="<%= PandoraFieldConstants.FIELD_SOKANDE_I %>" value="alla">Alla länder
        </td><td>
        <input type="radio" name="<%= PandoraFieldConstants.FIELD_SOKANDE_I %>" value="alla_utom_usa">Alla utom USA
        </td><td>
        <input type="radio" name="<%= PandoraFieldConstants.FIELD_SOKANDE_I %>" value="endast_usa">Endast USA
        </td><td>
        <input type="radio" name="<%= PandoraFieldConstants.FIELD_SOKANDE_I %>" value="land"><input type="text" size="12" value="<%= editPerson.getReferens() %>" name="<%= PandoraFieldConstants.FIELD_REFERENS %>">
        </td></tr>
        </table>
        </div>

Now the label row and the fields row don’t align the same so I want to remove the table and use divs instead for a better solution but when I use divs instead the fields display each on a new row. How should I do it? My CSS is

*  {font-family:arial;}

.avnamn{ 
                color: #90002b; 
                font-size: 140%; 
                display: inline; 
                vertical-align: 3%; 
                margin-left: 1%;
                }

.b{border:1px solid #000;}

.readonly{background-color: #CCC;}

.Webdings{
    font-family: Webdings;
    }

ul{margin-top: 0px}

.mt3{margin-top:-3px;}
.mt5p{margin-top:5px;}

.fontS80 {font-size: 80%;} 
a:link{color:#000; text-decoration:none; }
a:visited{color:#000; text-decoration:none; }
a:hover{color:#000; text-decoration:none; }
a:active{color:#000; text-decoration:none; }

.fontS75 {font-size: 75%;} 

.link{color: #003366;
    text-decoration: underline;
    cursor: pointer;
    font-weight: bold;}

.link_sm{color: #003366;
    text-decoration: underline;
    cursor: pointer;}

.link_sm{font-size: 70%;cursor: pointer;}

.small{font-size: 75%;}

.smallg{font-size: 75%;
color: #555;}

.ssmall{
    font-size: 65%;
    font-weight: bold;
    color: #555;
}
.small60{font-size: 60%;}
.small50{
    font-size: 50%;
    color: #333;
}
.smallb{font-size: 85%;}
table{display:inline;}

h1{font-size: 130%;display:inline;}
h2{font-size: 100%;display:inline;}
h3{
    font-size: 80%;
    display:inline;
    font-family: "Arial Unicode MS", Arial, Helvetica, sans-serif;
}
h4{font-size: 70%;display:inline;}
h5{
    font-size: 80%;
    display:inline;
    font-family: "Arial Unicode MS", Arial, Helvetica, sans-serif;
}

.hthin{
    font-size: 125%;
}

.th {text-align: left;}

td, th{font-size: 75%;
    vertical-align: text-top;}
.td_link{cursor: pointer;}
.td40{height:40px;}
.td60{height:60px;}



.thkant{
    border-top: 1px solid #000;
    border-bottom: 1px solid #000;
    font-size: 70%;
        text-align: left;
}

.labb{F0F0E3; c1c1b3 }

.bb{border-bottom: 1px solid #000;}
.bbV{border-bottom: 1px solid #FFF;}
.TB_nbA {background-color:#CCC;}
.TB_bt, .TB_nb, .TB_db, .TB_bb {background-color:#efefdc;}

.hk {background-color:#d9ddb3;}

.hknot {background-color:#f9faf2;}
/*<!--F8F8F1-->*/
.TB_bt{border-top: 1px solid #FFF;}
.TB_bt5{border-top: 5px solid #FFF;}
.TB_bb{border-bottom: 1px solid #999;}
.TB_bb2{border-bottom: 2px solid #c1c1b3;}
.TB_db{border-bottom: 1px solid #000; border-top: 1px solid #000;}
.TB_tb{border-top: 2px solid #efefdc;}

.TB_bo{border: 2px solid #efefdc;}
.TB_bo_hk{border-top: 1px solid #efefdc;}


.TB_bo2{border: 1px solid #efefdc;}

.TB_bo2B{
border-top: 2px solid #c1c1b3;
border-left: 3px solid #efefdc;
border-right: 3px solid #efefdc;
border-bottom: 2px solid #c1c1b3;
}

.TD_bo{
    border-right: 1px solid #c1c1b3;
    width: 9%;
    font-size: 70%;
    text-align: center;
}

.TD_bo2{

    border-right: 0;
    width: 9%;
    font-size: 70%;
    text-align: center;
}

.ytb{
    border-left:3px solid #efefdc;
    border-right:3px solid #efefdc;
}

.datum {
    font-size: 70%;
    padding-right: 5px;
    vertical-align: text-top;} 
.sub {background:#EAEAEA;}
.sub_meny, .sub_meny_r, .sub_meny_active, .sub_meny_sm{
    font-size: 70%;
    padding-left: 20px;
    padding-right: 20px;
    vertical-align: text-top;}

.sub_meny_sm {
    font-size: 60%;
    vertical-align: middle;
    padding-left: 10px;
    padding-right: 10px;
}   

.sub_meny_r{
    float:right;
    font-size: 70%;
    padding-left: 8px;
    padding-right: 8px;}

.sub_meny_rm{margin-top:4px;}
.sub_meny_active{font-weight: bold;}

.flikkant1 {
    background-image: url(../images/fl1k.jpg);
    background-position: center;
    z-index: -1;}

.inl_namn{
    font-weight: bold;
    font-size: 70%;
    color: Black;
    text-decoration: none;}

.th{text-align: left;}
.tr{text-align: right;}

.g1{
    background-color: #FFF;
    line-height: 20px;
}

.g2{
    background-color: #EEE;
    line-height: 20px;
}

.g3{
    background-color: #DCDCDC;
    line-height: 20px;
    font-weight: bold;
    font-size: 100%;
}
.g4{
    background-color: #CCC;
    line-height: 20px;
}

.popup{
    border-color: #000; 
    border-style: groove; 
    border-width: 2px; 
    padding: 0px; 
    background-color: #FFF;
    font-size: 70%;
}

.popuphandlaggare{ 
    border-color: #000;
    border-style: groove;      
    border-width: 2px;      
    padding: 0px;      
    background-color: #FFF;     
    font-size: 70%;     
    position: absolute;     
    top: 900px; 
    }

.popupN{
    background-color: #F0F0E3;
    color: #000;
    width: 100%;
    display: inline;
    font-weight: bold;
    height: auto;
    padding: 2px;
    border-bottom: 1px solid #000;
}
.pin{padding: 6px;}

.fl10, .fl20, .fl30, .fl40, .fl50, .fl60, .fl70, .fl80, .fl90, .fl100 {
    padding-bottom:4px;color: #000000;
}

.over{
    background-color: #EFEFDC;
    line-height: 20px;
}

.half{
line-height:50%;
}

.quarter{
line-height:25%;
}

.lh10{
line-height:10%;
}

.checkmargin {margin-right: 25px;}  
.checkmarginL {margin-left: 25px;}  

.pusher {padding-left: 15px;"}
.pusherR {margin-right: 40px;"}

.rand3{background-color: #FFF; line-height: 3px;}
.rand1{background-color: #FFF; line-height: 1px;}

.whiteborder {     color: #ffffff;      border: 4px solid #ffffff;      padding: 10px;      margin: 10px; }
#details { width: 700; color: #ffffff; }
.column1 {     color: #000000; margin: 0;      padding: 0;     width: 600px;     border:0;     float: left; }
.column2 {     color: #000000;margin: 0;      padding: 0;     border:0;     width: 80px;     float: right; }

.f200 {
    color: #000000;
}

.f210 {
    color: #000000;float: left;
}
.f220 {
    width: 400;
}
.f1450 {
    width: 800;
}
.f1550 {
    width: 150;
}
.b2{border:2px solid #efefdc;}
.inp_sel{width: 80%;}


.form-bg {
  background: #eeefdf;
  width:1000px;
  overflow:hidden;
}

.data-bar {
  border-bottom:1px solid #fbfbf7;
  display:inline-block;
  padding:10px 10px;
}

.left {
float:left;
width:200px;

}

.right {
float:right;
width:700px;
}

.data-box {
width:650px;
height:100px;
border:1px solid #cbcbcb;
}
#table td { 
margin:120px; 
} 

#personName {     float:left;     width:300px;   }  #otherDetails {     float:right;     width:450px;      }

.th_rad_sort {border-bottom: 2px solid #000000;}

a img { border: 0; outline:0;}

Answer by fesh

CSS

.fl20{float:left; padding:5px; margin:5px; width:120px;}
.fl450{float:left; padding:5px; margin:5px; width:450px;}

HTML

<div class="fl20">Personen är sökande i:</div>
<div class="fl450">
    <input type="radio" name="<%= PandoraFieldConstants.FIELD_SOKANDE_I %>" value="alla">Alla länder
</div>
<div class="fl450">
    <input type="radio" name="<%= PandoraFieldConstants.FIELD_SOKANDE_I %>" value="alla_utom_usa">Alla utom USA
</div>
<div class="fl450">
    <input type="radio" name="<%= PandoraFieldConstants.FIELD_SOKANDE_I %>" value="endast_usa">Endast USA
</div>
<div class="fl450">
    <input type="radio" name="<%= PandoraFieldConstants.FIELD_SOKANDE_I %>" value="land">
    <input type="text" size="12" value="<%= editPerson.getReferens() %>" name="<%= PandoraFieldConstants.FIELD_REFERENS %>">
</div>

Answer by Starx

I dont see why you need to use divs for something that would be more manageable in used with tables.

<table border="0" cellpadding="10"><tr><td>
        Personen är sökande i:   
        </td><td>
        <input type="radio" name="<%= PandoraFieldConstants.FIELD_SOKANDE_I %>" value="alla">Alla länder
        </td><td>
        <input type="radio" name="<%= PandoraFieldConstants.FIELD_SOKANDE_I %>" value="alla_utom_usa">Alla utom USA
        </td><td>
        <input type="radio" name="<%= PandoraFieldConstants.FIELD_SOKANDE_I %>" value="endast_usa">Endast USA
        </td><td>
        <input type="radio" name="<%= PandoraFieldConstants.FIELD_SOKANDE_I %>" value="land"><input type="text" size="12" value="<%= editPerson.getReferens() %>" name="<%= PandoraFieldConstants.FIELD_REFERENS %>">
        </td></tr>
</table>

Update: On that case still div, is not needed. See this demo using <ul> and <li>

Read more

Simplest way to prevent form reset method from clearing hidden inputs in IE8

Question by mikez302

The Javascript reset method for forms is not supposed to affect hidden inputs, but in IE8 and IE7 it does anyway. Does anyone know a way to make it behave properly?

I know I can do it by looping through individual inputs or by remembering the values before resetting the form and then restoring them but I am wondering if anyone can come up with a simpler way. Feel free to use jQuery if it will make your solution simpler. You can see an example at http://jsfiddle.net/Ddtz4/.

According to the author of the plugin, this is supposedly not an IE bug and the effect of the reset method on elements without initial values is undefined.

Answer by mikez302

I am thinking of something like this, using jQuery:

$(document).delegate("form", "resetForm", function() {
    $(this).find("input[type=hidden]").each(function() {
        $(this).prop("defaultValue", $(this).val());
    });
    this.reset();
});

Whenever I want to reset a form, I can do this:

$("#whateverForm").trigger("resetForm");

Answer by Starx

The following snippet seem to be working for IE8 too

$("#testInput").val('foo');
$("#testForm")[0].reset();
$("#result").html($("#testInput").val());

Update:

Why dont you create a custom reset function() instead:

$("#resetbutton").on("click", function() {
    var form = $(this).closest('form');
    form.find("input:not(:hidden)").val('');
    form.find("select").prop('selected', false);
    // so on

});
Read more

accessing parent property values in child class

Question by Eli

I have a strange issue where I set values in a parent class but cannot access those values in a child class extending the parent.

class Parent
{
    protected $config;

    public function load($app)
    {
        $this->_config();
        $this->_load($app);
    }

    private function _config()
    {
        $this->config = $config; //this holds the config values
    }

    private function _load($app)
    {
        $app = new $app();
        $this->index;
    }
}

class Child extends Parent
{
    public function index()
    {
        print_r($this->config); // returns an empty array
    }
}

$test = new Parent();
$test->load('app');

when I do that i get an empty array printed out. But if I do this then I am able to access those config values.

private function _load($app)
{
    $app = new $app();
    $app->config = $this->config
    $app->index;

}

and

class Child extends Parent
{
    public $config;
          ....
}

then I can access the config data from the parent.

Answer by Starx

You are accessing the values before anything is initialized there. First you have to set the values.

Example: call a method is the parent class, which set the value, on the contructor of the child class.

class Child extends Parent
{
    public function __construct() {
       $this -> setConfig(); //call some parent method to set the config first
    }
    public function index()
    {
        print_r($this->config); // returns an empty array
    }
}

Update: You also seem to confused about concept of OOP

class Parent { ..... }
class child extends Parent { ..... }
$p = new Parent(); // will contain all method and properties of parent class only
$c = new Child(); // will contain all method and properties of child class and parent class

But, you have to work with parent methods and properties just the same way you would do in the normal object.

Lets See another example:

class Parent { 
     protected $config = "config";
}
class Child extends Parent {
     public function index() {
           echo $this -> config; // THis will successfully echo "config" from the parent class
     }
}    

But another example

class Parent { 
     protected $config;
}
class Child extends Parent {
     public function index() {
           echo $this -> config; //It call upon the parent's $config, but so far there has been no attempt to set an values on it, so it will give empty output.
     }
}
Read more
May 3, 2012

COUNT() function each time, or store the value and increment it by one?

Question by john

I have a database with a user ‘votes’ table and a ‘user’ table. I’m thinking the database will get quite big in a small amount of time so i want to use the most efficient method.

I’m thinking i can either COUNT() the amount of votes with a WHERE statement from the ‘votes’ table every time, or i can store the score in the ‘user’ table and just increment it by 1 every time a vote is added.

Which would be best/quickest, and/or are there any other ways of doing this?

Answer by Starx

If you are thinking of the best way to do it. You have to look into optimizing and caching a lot.

I would say,
Create a column on the user tables to store cached score, but maintain the score on the separate table.

Whenever score changes operate of scores table and trigger an update on user’s table with the latest score result.

Doing this, you have extendability in your score data to, kind of like what stackoverflow uses for votes.

Read more

Search recent data with mysql, Full text search old data with solr

Question by paganotti

I build a web application in php and mysql. I want to search data doing full text search on 3-4 table. I’m considering for it lucene solr project.

My question is:
Is it correct save today data in separate mysql table, then and it make a simple search only for that today data and if it find enything, it try query on lucene for full text search but in old data( from yesterday to past) ?

My web app save data in mysql but I want that user can full text search on that data. I can run a job that 4 times for day import data from msql to lucene solr, but if data on mysql change by user and solr doesn’t already start importing and indexing data, user should be able in any case to search and see recent modified data.

What is the best approach for integrate lucene solr with mysql and php?

Is it possible send a modify command to solr ftom php for update data that user changed but be sure to commit data on solr only if a mysql
transaction go well done, so finaly i have commited data in mysql and commited data in lucene and if something goes wrong on web application rollback all as in mysql as in solr?

Answer by Starx

If you are thinking of implementing Lucene, the number of table does not matter. As per for the best approach, I would say to implement Zend’s Zend Search Lucene.

The lucene library like Zend Search Lucene have there own mechanism of tokenizing the data and search through them. They are basically fed with data from the database, from which they will build the search files and store them separately, if something goes wrong, then just rebuild the search.

About the possibility of update the Lucene data when the database gets updated, relies completely on what solution you implement. Zend Search Lucene on other hand, can do it.

Read more

How to prevent the clientside user from changing arguments in an onClick function?

Question by user1113531

I just realized while testing an onClick function with firebug that it would be really easy for a user to change the value of the arguments being passed. This could mess thins up.

Is there any easy way to prevent this, especially when arguments need to be passed?

Answer by Quentin

It is impossible. The code is executing on the user’s computer. They are in control.

If they edit it and “mess it up”, then that is on their head.

If they edit it and it submits an HTTP request to your server, and your server allows (for instance) that request to delete data belonging to another user then the problem is that your server didn’t check that the user submitting the request had permission to delete that data before following through.

Answer by Starx

No, this simply can’t be done.

Once the script is loaded to the client’s machine. He can use/modify it, as he wants.

Read more

how to form select query by range

Question by bonny

hello i have a search engine for my site. i have two selection fields. i would like to give an example:

input_a: 3
input_b: 5

so the queries should look like:

if ( ($input_a == true) && ($input_b == false) ){
        $filter_orders[] = " `col` LIKE '%$input_a%' ";
    } 
    if ( ($input_a == false) && ($input_b == true) ){
        $filter_orders[] = " `col` LIKE '%$input_b%' ";
    } 
    if ( ($input_a == true) && ($input_b == true) ){
        $filter_orders[] = " `col`= `col`>='%$input_a%' AND `col` = `col`<='%$input_b%' ";

now the problem is, that i dont know, if the last query is incorrect or not. the logic behind that will be that in case of my example the range between 3 and 5 should be found.

so 1,2 [3,4,5] 6,7,8…

if there is someone who could help me out i really would appreciate.

thanks a lot.

Answer by Starx

NO, sadly that is everything but correct. It should be something like this.

$filter_orders[] = " `col`>='%$input_a%' AND `col`<='%$input_b%' ";
Read more

is there a way to disable javascript debugging on a live server

Question by kayfun

Is there a way to tell debugging tools (firebug and the likes) never to be enabled for your website regardless of user’s browser configurations?

Answer by Konstantin Pribluda

no.

client is free to do whatever it sees fit with your javascript. You may try to obfuscate – but this is not effective against determined developers

Answer by Starx

Scripts, HTML, CSS, Images everything is loaded to the client before the page opens. So he can do anything he likes with what he has in his system. There is no way to disable it.

Even If there is a possibility of doing such, he can remove that as well.

So, basically its a wasted effort.

Read more
...

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