...

Hi! I’m Starx

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

How to change format of time

Question by user1304197

I have a $_POST ($_POST[‘durationChosen’]) which is a time and its format is ’00 Hrs 00 Mins 00 Secs’. Now I still want to display this format in the textbox but on the next page I want $_POST[‘durationChosen’] I want the format to be ’00:00:00′. How can I do this?

An example is in the textbox I have ’01 Hrs 30 Mins 15 Secs’ but in next page I want it formatted so its ’01:30:15′. Can this be done?

Answer by MichaelRushton

Try this:

<?php

  $array = sscanf($_POST['durationChosen'], "%u %s %u %s %u %s");

  $time = date('H:i:s', strtotime($array[0] . ':' . $array[2] . ':' . $array[4]));

Edit to reflect comment of acceptance below:

<?php

      $time = str_replace(array(' Hrs ', ' Mins ', ' Secs'), array(':', ':', ''), $_POST['durationChosen']);

Answer by Starx

You might want to read the manual. The problem with your coding is that, you are using short notation of time, which the data function does not understand. Read here

A simple way to get around this

$time = $_POST['durationChosen'];
$time= str_replace("Hrs", "Hour", $time);
$time= str_replace("Mins", "minutes", $time);
$time= str_replace("Secs", "seconds", $time);

$time = strtotime($time);
echo date($time, "H:i:s");
Read more

Apply background color to parent table th only

Question by Pavan Kumar

I have a table structure, where I can’t access jsp file to add class files. I have to manage it through css. In this case, I need to apply background color for 1st table all th’s. Not to nested table th’s. How can we do with css? Example : http://jsfiddle.net/qdDnJ/

Answer by Starx

The simplest way I know is to use the child selector

#yourtableId > tbody > tr > th { background: red; }

Demo

Read more

In Object Oriented Programming which object should maintain the many to many relationship? (if there is one)

Question by Matt

I’ll use an example to illustrate this:

class Company {

}

class Person {

}

Company and Person hold a many to many relationship. A Person can belong to multiple Companies and a Company can hold multiple People.

Would I then need to create a third class:

class CompanyPerson {

}

or should the company handle it:

class Company {
    function add_person() {

    }
}

or maybe the Person should?

class Person {
    function add_to_company() {

    }
}

Answer by Starx

This depends on the use case, but generally many to many object relationship can be deployed using a reference class

class CompanyPersonRelationship {
    public $company;
    public $person;
}

So now, both company and person can keeps track of their relationship lie

class Company {
   public $persons = array();   
}

class Person {
   public $companies = array();
}
Read more

MySql php: check if Row exists

Question by Jeff

This is probably an easy thing to do but I’m an amateur and things just aren’t working for me.

I just want to check and see if a row exists where the $lectureName shows. If a row does exist with the $lectureName somewhere in it, I want the function to return “assigned” if not then it should return “available”. Here’s what I have. I’m fairly sure its a mess. Please help.

function checkLectureStatus($lectureName)
{
 $con = connectvar();
 mysql_select_db("mydatabase", $con);
 $result = mysql_query("SELECT * FROM preditors_assigned WHERE lecture_name='$lectureName'");
  while($row = mysql_fetch_array($result));
  {
     if (!$row[$lectureName] == $lectureName)
     {
         mysql_close($con);
         return "Available";
     }
      else
     {
        mysql_close($con);
        return "Assigned";
    }
}

When I do this everything return available, even when it should return assigned.

Answer by quickshiftin

This ought to do the trick: just limit the result to 1 row; if a row comes back the $lectureName is Assigned, otherwise it’s Available.

function checkLectureStatus($lectureName)
{
    $con = connectvar();
    mysql_select_db("mydatabase", $con);
    $result = mysql_query(
        "SELECT * FROM preditors_assigned WHERE lecture_name='$lectureName' LIMIT 1");

    if(mysql_fetch_array($result) !== false)
        return 'Assigned';
    return 'Available';
}

Answer by Starx

Use mysql_num_rows(), to check if rows are available or not

$result = mysql_query("SELECT * FROM preditors_assigned WHERE lecture_name='$lectureName' LIMIT 1");
$num_rows = mysql_num_rows($result);

if ($num_rows > 0) {
  // do something
}
else {
  // do something else
}
Read more

preg_replace script, link tag not working

Question by john

I used the following code to remove script, link tags from my string,

$contents='<script>inside tag</script>hfgkdhgjh<script>inside 2</script>';
$ss=preg_replace('#<script(.*?)>(.*?)</script>#is', '', $contents);
echo htmlspecialchars($ss);

it works fine. But can I use anything that similar to html parsing rather than preg_match for this?

Answer by Starx

Here are few things you can do

  1. htmlspecialchars() can prove those tags useless
  2. striptags() removes all HTML tags

But the technique you are using is the correct one. However here is a improved version for that

echo preg_replace('/<scriptb[^>]*>(.*?)</script>/is', "", $contents);
Read more

Shrink-to-fit div and paragraph, based on image?

Question by colindunnn

The code below is a simplified version of my website. On my site, the image width varies from page to page and the text is around 100 words. That means the paragraph stretches the DIV to be wider than the image. Using only CSS, is it possible to shrink the DIV and the paragraph to the width of the image?

JSFiddle here

Example of what I’m trying to describe here. Top is what I’m getting, bottom is what I want.

HTML

<div>
    <img src="image.jpg" />
    <p>Lorem ipsum dolor sit amet.</p>
</div>

CSS

div {
   display: inline-block;
   }

Answer by mikevoermans

And for doing anything table related I shall forever shame myself: http://jsfiddle.net/WM6hK/3/

div {
display: table;
border: 1px solid red;
width: 1%;
}

p {
border: 1px solid blue;
}​

Answer by Starx

Right now its not possible, using CSS

Using jQuery, its as easy as this

$("div").width($("div img").width());

Demo

Read more
March 30, 2012

Embedding .swf files on an .html page

Question by Lily Evans

I’ve built an intro page for my website (it’s a .swf file) and I’m trying to embed it into an .html file, but the html code won’t allow me to resize the file (I want the file to take up the whole page, so I tried resetting the height and width parts of the object tag to 100% – it didn’t work)
someone told me that the best way to resize the file would be by using javascript. How would I do this?

the code is as follows:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="550" height="400" id="intro to elianas website" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="intro.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#1C140D" />
<embed src="intro.swf" quality="high" bgcolor="#1C140D" width="100%" height="100%" name="intro" align="center" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

Answer by Starx

You also need to provide the height and width for the Object as well.

<object width="100%" height="100&">
    <param name="movie" value="file.swf">
    ..
    <embed src="intro.swf" width="100%" height="100%" />
</object>
Read more

read name and value from every define('NAME','VALUE'); inside a .php file

Question by elcodedocle

I’m implementing a php interface to process a .php file containing a bunch of define sentences defining constants with text values to translate by somebody.

The input is parsed sentence by sentence and shown in the interface to the translator who will input the translation in a html textarea and send it to the server

By the end of the process an output file would be generated, identical to the input .php file, only with the define values translated.

E.g. processing an input file ‘eng.php’ like this:

<?php
define ("SENTENCE_1", "A sentence to translate");
?>

would give the translated output file ‘spa.php’ like this:

<?php
define ("SENTENCE_1", "The same sentence translated to spanish");
?>

For this I would like to know:

1) What is the best way to parse the input file to get the constant names and values in an array? (Something like $var[0][‘name’] would be “SENTENCE_1” and $var[0][‘value’] would be “A sentence to translate”)

2) Would it be possible to get the translation from google translator shown in the input textarea as a suggestion to edit for the person who is translating it? How? (I read google translator api v1 is no longer available and v2 is only available as a paid service. Are there any free alternatives?)

Answer by Starx

Use get_defined_constants() to get the list of all the defined constants.

To get userdefined constant specially

$allconstants = get_defined_constants(true);
print_r($allconstants['user']);
Read more

My <body> isn't accepting background color/image

Question by langel

CSS code

body { 
    background-attachment: scroll;
    background-clip: border-box;
    background-color: #ececec;
    background-image: none;
    background-origin: padding-box;

}

Link: http://grupocoral.netai.net/

No matter what I do I can’t change background properties.

Answer by Engineer

Remove <style type="text/css"> and </style> from your css file.

Answer by Starx

You have placed your CSS code in the style sheet along with the <style> tags. You shouldn’t do that when attaching an external CSS stylesheet. You can directly define the styles.

Remove

<style type="text/css"> & </style> from your file style.css

Read more

jQuery.html() not working well with Asp.Net innerHtml

Question by Umm….

So basically, I have a div which is runat=”server”

<div id="results" runat="server" class="results"></div>

Now, I add HTML to this using jQuery.

$('.results').html('Some crazy data');

However, when I try to access the data in this DIV using C#, it says the DIV has no contents.

string myResults = results.innerHtml;

myResults is empty.

Can someone help me out? I’ve never seen this behavior before.

Many Thanks!

Answer by amit_g

You may be looking for something like this…

<div id="results" runat="server" class="results"></div>
<input type="hidden" id="hiddenResults" runat="server" class="hiddenResults" />

$('.results').html('Some crazy data');
$('.hiddenResults').val('Some crazy data');

Now you can access the data on server

string myResults = hiddenResults.Value;

Answer by Starx

Instead of mixing up jQuery and Javascript. Try

var myResults = $(".results").html();

Just to verify everything is correct, see if your results is defined as below

var results = document.getElementById("results");
var myResults = results.innerHtml;

If you trying to get the value of results to the server, it is not possible through jQuery or Javascript unless send an AJAX request.

Read more
...

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