...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page

How can I exclude first <tr>

Question by mustafa

With php dom, I am getting all TRs and TDs inside.

<?PHP
foreach($table->getElementsByTagName('tr') as $tr){
   echo $tr->getElementsByTagName('td')->item(0)->nodeValue);
}
?>

Can we exclude the first tr and its tds.

Answer by Michael Berkowski

I think the node list is numerically indexed (though I cannot test it), so using a key in the foreach may work:

foreach($table->getElementsByTagName('tr') as $key => $tr){
   // Don't act on the first element
   if ($key > 0) {
     echo $tr->getElementsByTagName('td')->item(0)->nodeValue);
   }
}

Answer by Starx

Compare the index of elements, if its first and use continue the to skip the loop.

<?PHP
foreach($table->getElementsByTagName('tr') as $index => $tr){
   if($index == 0) { continue; }
   echo $tr->getElementsByTagName('td')->item(0)->nodeValue);
}
?>
Read more

CSS – context used styling?

Question by Jurik

I thought that it was possible, but everyone tells me it’s not.

I want context styling in my css file like:

div#foo {
 h2 {
   color: #F42
 }
 p.bar {
   font-size: 12px
 }
}

So that only h2 and p.bar in the div with id foo will be styled. Or is this only possible with LESS and other similar libs?

Thanks & kind regards,
Jurik

Answer by VAShhh

This is not possible with pure CSS, that’s why you should use SCSS or LESS (i suggest to use SASS/SCSS), which are CSS supersets

LESS/SASS-SCSS allows you to write dynamic CSS with ease, take a look at this comparision

check out COMPASS which is the main reason why I suggest you SASS/SCSS

Answer by Starx

Its not possible using default CSS techniques.

But, by using sass and less however, it is possible.

The code in your question, works in both of the libraries above.

Read more

input[type='text'] CSS selector does not apply to default-type text inputs?

Question by Yarin

The default input type is ‘text’. I’ve always assumed then that css declarations targeting input[type='text'] would affect those inputs even if the type was not explicitly declared on the control. However, just noticed that my default-type text inputs don’t get the styles. Why is this the case?

CSS:

input[type='text']
{
background:red;
}

HTML:

<input name='t1' type='text'/> /* Is Red */
<input name='t1'/> /* Is Not Red */

http://jsfiddle.net/LhnNM/

Answer by Mr Lister

The CSS uses only the data in the DOM tree, which has little to do with how the renderer decides what to do with elements with missing attributes.

So either let the CSS reflect the HTML

input:not([type]), input[type="text"]
{
background:red;
}

or make the HTML explicit.

<input name='t1' type='text'/> /* Is Not Red */

If it didn’t do that, you’d never be able to distinguish between

element { ...properties... }

and

element[attr] { ...properties... }

because all attributes would always be defined on all elements. (For example, table always has a border attribute, with 0 for a default.)

Answer by Starx

Because, it is not supposed to do that.

input[type=text] { } is an attribute selector, and will only select those element, with the matching attribute.

Read more

How to add image button to a link using html in javascript

Question by rcky

The image button is not working perfectly.. any suggestions ?

The current code on which i am working is:-

var generator = window.open (); 
 generator.document.write('<html><head><title>Pop uP</title>');
generator.document.write('<p style="color:#C52B27;">');
generator.document.write(message);
 generator.document.write('</p>');
generator.document.write('</head><body>');

 generator.document.write('<a href="javascript:self.close()"><img src="/img/save_orange.gif" border=0"></a>');
generator.document.write('</body></html>');
generator.document.close();

Answer by Starx

Bind a js snippet to onclick event of the image

<img src="/img/save_orange.gif" border="0" onclick="self.close()">

According to this doc, self.close() only works if the document is open by window.open() method. So, on this case you might want to use windows.close()

Read more

How to combine both array and insert into database php

Question by vvr

if only one array is there for example

$values = array(x, y, z);

i am adding them into database like this

    foreach ($values as $value)
    {
        $insertFunction = addValues($value);
    }

my arrays:

$array1 = Array ( 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 );

$array2 = Array ( fb1, or1, fb2, or2, fb3, or3, fb4, or4, fb5, or5 );

But i want both array to combine and insert them into database.
How can i do this please help me

Updated:

When i am printing the POST values i am getting out put like this

Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 2 [4] => 1 [5] => 2 [6] => 1 [7] => 2 [8] => 1 [9] => 2 )

Array ( [0] => fb1 [1] => or1 [2] => fb2 [3] => or2 [4] => fb3 [5] => or3 [6] => fb4 [7] => or4 [8] => fb5 [9] => or5 )

when i tried with array_merge my out put is like this

Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 2 [4] => 1 [5] => 2 [6] => 1 [7] => 2 [8] => 1 [9] => 2 [10] => fb1 [11] => or1 [12] => fb2 [13] => or2 [14] => fb3 [15] => or3 [16] => fb4 [17] => or4 [18] => fb5 [19] => or5 ) 

How to insert them in separate columns in a table $array1 and $array2

my database table is like this

1.id
2.username
3.network_id

id is primary key
network_id values coming in array1
username values coming in array2

Answer by Ofir Baruch

EDIT:

After you mentioned seperated columns I think I understand what you’re looking for:
I’m assuming that array1 and array2 are in the same size.

for($i = 0; $i < count($array1); $i++)
{

  $array2[$i] = (int)$array2[$i]; //"validating" the username (an integer)
  mysql_query("INSERT INTO yourTableName (`username`,`network_id`) VALUES('".$array2[$i]."','".$array1[$i]."')");
}

Result:

tblName:

username: 1 2 1 …

network_id: fb1 or1 fb2 …

Is that what you were looking for?

Ignore this and merging:

    $combined = array_merge($array1 , $array2);

   //$combined = Array ( 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,fb1, or1, fb2, or2, fb3, or3, fb4, or4, fb5, or5 );

Answer by Starx

You can use array_merge(), function to merge multiple array into one.

$arrays = array_merge($array1 , $array2);
foreach ($arrays as $value)
{
    $insertFunction = addValues($value);
}
Read more

Conditional CSS in CSS not working

Question by Dave

so I have this css code:

.onetoone{
    position: relative;
    margin: 10px auto 5px;
    padding: 20px;
    width: 878px;
    [if ! IE] height: 1241px;
    [if IE] height: 241px;
    border: 1px solid #AAAAAA;
    behavior: url(PIE.htc);
}

However I don’t see any different height on .onetoone in IE and Firefox, both are still have the same 214px height. It should has a 1232px height in firefox and 241px height in IE 8, right?

What did I miss?

Thanks for all your answers.

Answer by Starx

Have you read the usage file?

According the website, you have to write the css and compile it using its online tool and then you have include c-css.php to get it working.

@import "/media/css/c-css.php";

According to example from this website, this should fix the problem.

.onetoone{
    position: relative;
    margin: 10px auto 5px;
    padding: 20px;
    width: 878px;
    border: 1px solid #AAAAAA;
    behavior: url(PIE.htc);
}
[if !IE] .onetoone {
    height: 1241px;
}

[if IE] .onetoone {
    height: 241px;
}

After you compile it becomes a huge file like this one here and you include like the way I said above.


However, I would go with the good old fashioned way I knew. And it was to include different css with the conditional CSS comments

<!--[if IE]>
   <style>
      .onetoone {
        height: 241px;
       }
   </style>
<![endif]-->

<!--[if !IE]>
   <style>
      .onetoone {
        height: 1241px;
       }
   </style>
<![endif]-->

Should work either way.


Update

Instead of loading different style like the one in my example load different file in the end after loading all the main css files in this way.

<!--[if IE]>
<link rel="stylesheet" type="text/css" href="fixforie.css" />
<![endif]-->
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="fixforie6.css" />
<![endif]-->
Read more

How can i change an inline css style when clicking a link?

Question by Lucas Matos

i have a form hidden with inline style="display: none;"

how can i dinamicaly change this style to style="display: inline;" when a link on the page is clicked?

Answer by Starx

Prety simple

<a href="#" onclick="document.getElementById('myform').style.display = 'inline';">Click me</a>

Update


jQuery is a lightweight JavaScript library that can do tons of cool stuff, with writing a very less script from the developers side.

First of all, I suggest you to read “How jQuery works?“, is has everything you need to get started using the jQuery.


I will explain the code I wrote in the fiddle.

First of all the link & form

<a id="linktotoggle" href="#">Click Me</a>
<form id="formtotoggle"></form>

Remember the id in the link and form above. That is how we are going to select the element in the script just like what document.getElementById() does.

Lets hide the form by default

#formtotoggle { display: none; }

Now lets write the jquery

$(document).ready(function() {
// ^ This is an event, which triggers once all the document is loaded so that the manipulation is always guaranteed to run.
   $("#linktotoggle").click(function() {
   // ^ Attach a click event to our link
        $("#formtotoggle").toggle();
        // ^ select the form and toggle its display

   });
});

Hope it is enough, to get you started.

Read more

How do I Measure distance using php?

Question by Justin Petty

I should start of by saying that I’m still pretty green when it comes to PHP, so please be patient with me. I’m trying to figure out the distance between two latitudes and longitudes using php. I found a bunch of scripts online but, however when I try to test them I get the same distance no matter what latitudes and longitudes I use. I’m sure it’s something probably on my part, so I’ll just post what I have so far.

$lat1 = "29.140762";
$lon1 = "-91.639243";

$lat2 = "29.136275";
$lon2 = "-91.635524";

function distance($lat1, $lon1, $lat2, $lon2, $unit) {
    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $unit = strtoupper($unit);

    if ($unit == "K") {
        return ($miles * 1.609344);
    } else if ($unit == "N") {
        return ($miles * 0.8684);
    } else {
        return $miles;
    }
}

// Miles
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "m") . " miles<br><br>";

//Kilometers
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "k") . " kilometers<br><br>";

//Nautical miles
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "n") . " nautical miles";

Answer by rwilliams

I’m guessing that you’ve hard coded your inputs and are just changing the variables at the top of the page and expecting new results. Those variables don’t do anything currently. Try the changes I’ve made below to your function calls, now the variables will be recognized.

// Miles
echo distance($lat1, $lon1, $lat2, $lon2, "m") . " miles<br><br>";

//Kilometers
echo distance($lat1, $lon1, $lat2, $lon2, "k") . " kilometers<br><br>";

//Nautical miles
echo distance($lat1, $lon1, $lat2, $lon2, "n") . " nautical miles";

Answer by Starx

NO it does not

Just tested it and outputs

262.67779380543 miles

422.73893139401 kilometers

228.10939614064 nautical miles

Update

Here is another output when using 
// Miles
echo distance(35.9697, -96.80322, 29.46786, -98.53506, "m") . " miles<br><br>";

//Kilometers
echo distance(35.9697, -96.80322, 29.46786, -98.53506, "k") . " kilometers<br><br>";

//Nautical miles
echo distance(35.9697, -96.80322, 29.46786, -98.53506, "n") . " nautical miles";

460.32704312505 miles

740.82456489105 kilometers

399.7480042498 nautical miles

Read more

CodeIgniter outputting multiples of the same row

Question by Michael Grigsby

How can I keep the foreach statement from outputting multiple’s of the same row? I need it to just output all the rows that meet the Sql requirement one time. Any ideas?

$query  = $this->db->query("SELECT * FROM churchMembers WHERE cMuserId = '{$userid}'");
$row = $query->row();
$membersChurchId = $row->cMchurchId;
$query = $this->db->query("SELECT wp.entryData, u.firstname, u.userid
                           FROM wallPosts wp, users u
                           WHERE wp.wpChurchId = '{$membersChurchId}'");
foreach ($query->result() as $row) {
    echo $row->entryData.nl2br("n");
}

Answer by Starx

Update

After discussing the problem, it was the problem with the query.
The correct query in your case will be

$query = $this->db->query("SELECT wp.entryData, u.firstname, u.userid FROM users u INNER JOIN wallPosts wp on wp.postingUserid = u.userid WHERE wp.wpChurchId = '{$membersChurchId}'");

I think you are approaching this the wrong way.If you need to output all the rows, that meet your requirements, at one time, then you have to create a function, which reads the rows and build the output. But still you will be using the foreach

function buildAll($result) {
     $output = "";
     foreach($result as $row) { 
         $output .= $row->entryData."<br />"; //no need for nl2br("n")
     }
     return $output;
}

Now include this function in a helper or whatever you like as use it in your code like

$query = $this->db->query("SELECT wp.entryData, u.firstname, u.userid
                           FROM wallPosts wp, users u
                           WHERE wp.wpChurchId = '{$membersChurchId}'");

echo buildAll($query -> result()); //in one show

However the method I wrote above is only good, when you are using the function in more than one occasion

Read more
...

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