...

Hi! I’m Starx

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

Adding an EJB to a web application in netbeans

Question by henry joseph

I was following a tutorial on how to create a web application in netbeans using Java EE 6. The tutor added a new bean by just right clicking on the project name->new->session bean. When I tried to follow the same instruction, I didn’t find the session bean when I went to new. I am using netbeans 6.9.1. Is there a way to add it ?

Thanks

Answer by Starx

You can add, the session beans, by using project pane on the left side. (If this is not showing, Press Ctrl + 1 or go to windows -> select Projects)

Then to add the session beans

  • Right your project
  • Go to New
  • Find Session Beans, If not go to others
  • There find a category called “Enterprise JavaBeans”
  • Select Session Bean
  • Continue and complete the wizard.
Read more
June 26, 2012

How to restrict the div to margin right or left

Question by Rana Muhammad Usman

I have created a custom scroll. In which I am shifting the contents right and left by increasing or decreasing the margin. I want to restrict the div not to margin more when the last or first content is visible or you can say I want to make it like scroll. How I can restrict the div not to move anymore

$('#left').live('click' , function(){
$('#myDiv').css("margin-left","-=10px");

})
$('#right').live('click' , function(){
$('#myDiv').css("margin-left","+=10px");

})

JS Fiddle

Answer by Starx

Define and maximum limit for margin left or right, then use this value to compare before changing its position.

var maxMargin = 500, minMargin = 20;

$('#left').on('click' , function(){
    if($('#myDiv').css("marginLeft") > minMargin) {
        $('#myDiv').css("margin-left","-=10px");
    }
});

$('#right').on('click' , function(){
    if($('#myDiv').css("marginLeft") < maxMargin) {
        $('#myDiv').css("margin-left","+=10px");
    }
});

And use .on() rathan than .live(), as it has been deprecated.

Read more

How to set this if isset function

Question by Danny Florian

I have the following code that sets a background if user has uploaded to database. If user has NOT uploaded an image then the result is a blank img src=”

I need to set this as an if isset function so I can plug in an alternate image if user has not uploaded anything.

Here is the current code:

<div id="background"><?php echo isset($background_image) && file_exists(ROOT.$background_image)?"<img src='$background_image' alt='' />":'';?></div>

Answer by Eoghan

Your code’s a little dirty, opening php and closing it mid-html tag is only going to make it confusing for you in the future.

You’re echoing back an isset which is just echo’ing back a boolean.

Try this;

$background_image = ""; // Not sure what you're using here - their username? Dump it in here anyway.
if (file_exists($background_image))
{
    echo "  <div id="background">
            <img src="{$background_image}" alt="" title="" />
        </div>";
}

Hope this helps.

  • Eoghan

Answer by Starx

Is it really neccessary to set the blank source of the image?

But a understandable and corrected code of what you are attempting is this

<div id="background">

    <?php
    echo "<img src='"; 
        echo isset($background_image) && file_exists(ROOT.$background_image) ? $background_image : '';
    echo "' alt='' />";
    ?>

</div>

The problem was that, either you were echo entire <img> tag, or just display ' '(Blank) with attached endings.

The short form:

    echo "<img src='".(isset($background_image) && file_exists(ROOT.$background_image) ? $background_image : '')."' alt='' />";
Read more
June 25, 2012

PHP Populate Text box from Select – Array fields

Question by user1479891

I need to populate the relevant text box with the results of a Select drop down.

My Javascript so far is

<script language="JavaScript">
var mytextbox = document.getElementById('error');
var mydropdown = document.getElementById('errormsg_id');

mydropdown.onchange = function(){
     mytextbox.value = this.value;
}
</script>

My select box and text boxes are built as arrays from a query so there is nultiple of them.
I need to be able to populate the corresponding text box with the results from the Select next to it.

Any ideas?

Thanks

    <td width="1%">

    <select style="width:100%" name="errormsg_id[]"  id="errormsg_id[]">

    <?php do { ?> 

    <option value="<?php echo  $row_rserrormsg['errormsg_id']?>"
    <?php if ($row_rsdates['errormsg_id'] == $row_rserrormsg['errormsg_id'])                 {              echo("selected='selected'"); } ; ?> >
<?php echo $row_rserrormsg['error_message']?></option>

<?php } while ($row_rserrormsg = mysql_fetch_assoc($rserrormsg)); ?>

<?php  mysql_data_seek($rserrormsg, 0);
$row_rserrormsg = mysql_fetch_assoc($rserrormsg);?>
</select>
</td>  

<TD  align="center" class="adminuser">
<input style="width:96%;text-align:left;"  autocomplete="on"  title="Enter Error    Message" type="text" name="error[]" id="error[]" value="<?php echo $row_rsdates['error_message']; ?>" maxlength="100"/> 
</TD>

Answer by Starx

You need to think of way to uniquely identify your corresponding set of elements. The most convenient way I would suggest would be to use the primary key value of the data row.

And example would be like

while($row = mysqli_fetch_assoc($result)) {
   $id = $row['id']; //A primary key field


   echo '<input type="text" name="text'.$id.'" id="text'.$id.'" />';
   echo '<select name="select'.$id.'" id="select'.$id.'" />...</select>";
}

Next, call a function from the select menu, so that only the relevant text box is updated. Update the above select portion to read something similar to this.

echo '<select name="select'.$id.'" id="select'.$id.'" onchange="updateBox('text'.$id.'', this.value)" />...</select>";

Now, create a very simple simple function to update the text box.

function updateBox(element, value) {
    el = document.getElementById(element);
    el.value = value;
}
Read more
June 21, 2012

Join tables from two different server

Question by Gulrej

I have two different server server1 and server2, now I have db1 in server1 and db2 in server2.
I am trying to join these two table in MySQL like this.

Select a.field1,b.field2  
FROM  [server1, 3306].[db1].table1 a  
Inner Join [server2, 3312].[db2].table2 b  
ON a.field1=b.field2  

But I am getting error. Is is possible in MYSQL.

Answer by Starx

Yes, it is possible in MySQL.

There are similar questions asked previously too. You have to use FEDERATED ENGINE to do this. The idea goes like this:

You have to have a federated table based on the table at another remote location to use the way you want. The structure of the table have to exactly same.

CREATE TABLE federated_table (
    id     INT(20) NOT NULL AUTO_INCREMENT,
    name   VARCHAR(32) NOT NULL DEFAULT '',
    other  INT(20) NOT NULL DEFAULT '0',
    PRIMARY KEY  (id),
    INDEX name (name),
    INDEX other_key (other)
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://fed_user@remote_host:9306/federated/test_table';

[Source Answer]

Read more

HTML5 – Suggestions for where to start from

Question by Vivekanand

I want to look into developing mobile applications using HTML5. I’m very new to it and do not have any idea about HTML5. I would like to take some suggestions about where to get the basic tutorials for starters and what is the best IDE to use.

Answer by Starx

If you are still learning the basics HTML5 then HTML5 Doctor is a very good reference. It tell you about all the semantic meaning of tags and the correct use of it.

Next, look into a quick start tutorial from sitepoint which will get you started.

Read more
June 18, 2012

Can't get why background image doesn't look the same?

Question by DenMed

I’m using jQuery validation plugin.
I have two images: first background of label, when data is valid, and second – invalid.

But they don’t look the same with same css attributes.

Here screenshots of my trouble screenshot

Here is my code in css file:

 label.error {
color:transparent;
display: inline;  
background: url('../images/not_valid.png') no-repeat;
padding: 10px;
margin-left: 5px;
width: 47px;
height: 36px;
}

label.valid {
background: url('../images/valid.png') no-repeat;
display: inline;  
padding: 10px;
margin-left: 5px;
color:transparent;
width: 47px;
height: 36px;
}

EDITED: FILES ON DROPBOX link

Answer by Starx

Well, the image seems to be cropped. So the problem seems to be related to either background-position or dimensions or size of the label.

Read more

about editing only 1 row in a table

Question by Triveni Gaikwad

How to edit a row in mysql using php.
I have a table , i will fetch it into php page to display the table in html format and at the end i have a edit button when i click edit button then the row should be editable and i can enter new values and that should update in the same table row.. after c licking edit button the button should change to submit/update…

This is the image of the table, the values here are in text boxes but readonly when the user clicks first edit the first row should be writable and the entered values should store

Answer by Starx

This is one of the basic actions, which falls into the category of CRUD functions. Here are the few steps you need to know to get you started.

  • Pass a unique id of the record a edit page or class.
  • You may attach the id, to the edit link of each page.
  • This page will use this id and fetch the record from the database
  • The details will be used to fill up of a form, from which the user will update the records.
  • The form will also keep the record of the id in most probably a hidden element.
  • Next, when this edit form is submitted, a processing will create a UPDATE query based on the data and execute them.
Read more

Creating an Array with PHP and MySQL

Question by user1114330

I have looked at several posts on stackoveflow for creating an array and haven’t found what I was looking for yet…so many different answers I would like to attempt and get one clear that is closest to what I am trying to accomplish.

Say I want to create an array using this query:

“select * from products”

How is this accomplished most efficiently?

Thank you.

PS – note that I am starting from scratch.

UPDATE

My config file:

`[root@CentOS testphp]# vi config.php
<?php
// Connection's Parameters
$db_host="localhost";
$db_name="tablename";
$username="username";
$password="password";
$db_con=mysql_connect($db_host,$username,$password);
$connection_string=mysql_select_db($db_name);
// Connection
mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);
?>`

The php code I am trying to create the array with…I am getting a Syntax Error:

Parse error: syntax error, unexpected T_VARIABLE in /var/www/html/testphp/test.php on line 2

I get the same error:

`[root@CentOS testphp]# vi test.php
<?php include('config.php')
$query = "select * from upload_products";
$dataArray = array();
$result = mysqli_query($query, $link);
while($row = mysqli_fetch_assoc($query)) {
$dataArray[] = $row;
}
var_dump($dataArray); //Now you have your array.
?>`

Any ideas…It feels like I am missing something here. And, yes I have read the documentation…used their code line by line, reviewed the code and still get the same error as I do with all the code examples I have found on the web.

Answer by Starx

There is not definite way to convert a database result object into an associative array directory. You will have to create this array your result.

$query = "...";
$dataArray = array();
$result = mysqli_query($query, $link);
while($row = mysqli_fetch_assoc($query)) {
    $dataArray[] = $row;
}
var_dump($dataArray); //Now you have your array.
Read more

Jquery – need help on usage

Question by Donald

Is this the right way of using the php script in the jquery function?
The pathToTabImage variable

<script type="text/javascript">
    $(function(){
        $('.slide-out-div').tabSlideOut({
            tabHandle: '.handle',                     //class of the element that will become your tab
            pathToTabImage: '<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/images/ack.jpg';, //path to the image for the tab //Optionally can be set using css
            alert(pathToTabImage);
            imageHeight: '122px',                     //height of tab image           //Optionally can be set using css
            imageWidth: '125px',                       //width of tab image            //Optionally can be set using css
            tabLocation: 'right',                      //side of screen where tab lives, top, right, bottom, or left
            speed: 600,                               //speed of animation
            action: 'click',                          //options: 'click' or 'hover', action to trigger animation
            topPos: '600px',                          //position from the top/ use if tabLocation is left or right
            leftPos: '550px',                          //position from left/ use if tabLocation is bottom or top
            fixedPosition: false                      //options: true makes it stick(fixed position) on scroll
        });

    });

Answer by Starx

It is one of the available ways to inject PHP codes, inside as HTML, CSS, or JS Document/Snippet. However, syntactically, there are few errors.

PHP Error

<?php echo $this->baseurl ?>

No selicolon on the end statement

JavaScript Error

pathToTabImage: '...';, //...

Unneeded semicolon, ending a statement in an incorrect way

alert(pathToTabImage);

Injecting an independant statement inside another statement

Read more
...

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