0

I am trying to pull some information from an application and then creates table rows based on this array, and I am lost in the middle of this code I need help.

Submit code is :

<?php
session_start(); // NEVER forget this!
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("webauth", $con);
$myusername = $_SESSION['myusername'];
//$result = mysql_query("SELECT * FROM user_pwd");
$sql5 = ("SELECT dept from `user_pwd` WHERE  name = '$myusername'");
$result5=mysql_query($sql5);
$row5=mysql_fetch_array($result5);
// echo "<br />";

mysql_close($con);
$url3 = 'http://localhost:1090/WebEmpLoad.hal';
$myvars3 = 'department='.$row5['dept'];
$ch3 = curl_init( $url3 );
curl_setopt( $ch3, CURLOPT_POST, 1);
curl_setopt( $ch3, CURLOPT_POSTFIELDS, $myvars3);
curl_setopt( $ch3, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch3, CURLOPT_HEADER, 0);
curl_setopt( $ch3, CURLOPT_RETURNTRANSFER, 1);
$response3 = curl_exec( $ch3 );
//echo $response3;
$arr = (explode(',',$response3,100));
$Num = sizeof($arr);
$rows = ($Num - 1)/2;
echo $rows;
?>

and I am trying to create raw based on the value of $arr.

Check the code and scripts here.

4
  • i managed to convert the string to array. Commented Apr 24, 2013 at 10:22
  • creation of new rows based on the data pulled,that what is not working. Commented Apr 24, 2013 at 10:23
  • See w3schools.com/php/php_mysql_insert.asp for example. It uses mysqli, but it is almost the same with mysql_ functions. Commented Apr 24, 2013 at 10:42
  • dear Adder,the data which i am trying to create rows for,is an array.as you can see in the fiddle,i am trying to create rows filled with Emp code and Name values.depends on the records pulled same number of rows should be created and populated with array values. Commented Apr 24, 2013 at 10:46

5 Answers 5

1

There are a few things that can be done to improve your script. Since your question is so broad I will point a few of them out..

First..

$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

Can be shortened to simply

$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());

Second, you forgot to add the resource parameter in your mysql_query(). Though this parameter is optional, by the way you've got your variable names ($sql15) I assume you may have multiple connections.

$result5=mysql_query($sql5, $con);

Also, I am not sure what you're doing here..

$sql5 = ("SELECT dept from `user_pwd` WHERE  name = '$myusername'");

your syntax may be perfectly valid for all I know, but I've never seen a string defined like this before and a quick Google search didn't show me anything.. so you may want to change that to just..

$sql5 = "SELECT dept from `user_pwd` WHERE  name = '$myusername'";

Next, you close your connection before you call your data. You might want to move the mysql_close() down to a line that is AFTER $myvars3 = 'department='.$row5['dept'];

Oh, and you're using mysql_fetch_array() and then referring to it as an associative array ($row5['dept']). To be safe, either change $row5=mysql_fetch_array($result5); to $row5=mysql_fetch_assoc($result5); or add the associative array result type like so.. $row5=mysql_fetch_array($result5, MYSQL_ASSOC);

And finally we'll address the uploading of your data..

I don't know how you've got it worked out but you're basically taking a webpage and breaking it into an array everwhere there is a comma and trying to upload the array into a table. Unless you know EXACTLY how many commas appear on that page and you know that the number matches the number of columns in your table this is a really awful idea. I don't know what you're trying to accomplish but I assure you, there is a better way.

And to answer you actual question.. the SQL syntax to insert a row is "INSERT INTO table (column1, column2) VALUES ('value1', 'value2')";

FINAL NOTE.. You're using an old API, switch to PDO. SEE BIG RED WARNING here

So, to put it all together...

session_start(); // NEVER forget this!
$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db("webauth", $con);
$myusername = $_SESSION['myusername'];
//$result = mysql_query("SELECT * FROM user_pwd");
$sql5 = "SELECT dept from `user_pwd` WHERE  name = '$myusername'";
$result5=mysql_query($sql5, $con);
$row5=mysql_fetch_assoc($result5);
// echo "<br />";

$url3 = 'http://localhost:1090/WebEmpLoad.hal';
$myvars3 = 'department='.$row5['dept'];
mysql_close($con);
$ch3 = curl_init( $url3 );
curl_setopt( $ch3, CURLOPT_POST, 1);
curl_setopt( $ch3, CURLOPT_POSTFIELDS, $myvars3);
curl_setopt( $ch3, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch3, CURLOPT_HEADER, 0);
curl_setopt( $ch3, CURLOPT_RETURNTRANSFER, 1);
$response3 = curl_exec( $ch3 );
//echo $response3;
$arr = (explode(',',$response3,100));
$Num = sizeof($arr);
$rows = ($Num - 1)/2;
echo $rows;
Sign up to request clarification or add additional context in comments.

8 Comments

Dear Tuna Fish, thanks for your valuable comments,i fixed my code accordingly. you're right it's just i am kind of newbie to this.
Dear Tuna Fish, thanks for your valuable comments,i fixed my code accordingly. you're right it's just i am kind of newbie to this. one more thing : i am not trying to insert a database table rows,it's just a normal HTML table and i need it to be filled automatically based on the information pulled. for example the array which i pulled is : "Array ( [0] => AKH0031 [1] => TONY GRACE THOMAS [2] => AKH0036 [3] => AHMED K A ABUNASSER [4] => AKH0043 [5] => SUDAD IBRAHEEM ABDULQADER AL JUBOURI [6] => AKH0068 [7] => BINDU NOOROMAKAL RAJAN [8] => AKH0100 )"
so according to that i need some code to create 4 rows filled with Employee code and Employee Name accordingly.
Guys start using mysqli_ instead of mysql_, since it is deprecated as of PHP 5.5.0, and it'll be removed after that.
@elavarasanlee You're right, I mentioned that in the answer. However PHP6 is not even in the production stages. It will be years before that is a real issue. Besides PDO is the new standard, not mysqli.
|
0
AS use have array of data.
user the code where your want to print the rows from this array
**<table>**
<?php 
foreach ($arr as $rows)
{
echo "<tr><td>".$rows."</td></tr>";
}
?>
**</table>**

1 Comment

Dear Kuldeep, appreciate your feedback,your idea sounds terrific,but what i need is kind of different, i need to create rows where the values of some cells are the values of the array,so it's more of automatic expanding table.
0

Okay, I see what you're doing.

Try this out.

echo "<table>";
$col = "left";
foreach($rows as $row){
    if($col == "left"){
        echo "<tr><td>$row</td>";
        $col = "right";
    }else{
        echo "<td>$row</td></tr>";
        $col = "left";
    }
}
echo "</table>";

5 Comments

i tried that and i got this warning : Warning: Invalid argument supplied for foreach()
@AhmedAli Woops, you I was a little confused. Change foreach($rows as $row) to foreach($arr as $row) ..or just change $rows to whatever your array is
the thing is that have a defined table: because i am using some special drop down menus
I am still not sure exactly what you mean. If you still need help you can post your entire code here and I'd be happy to take a look.
@AhmedAli can you print_r($row5); and post the output somewhere so I can see what you're working with. Hard to fix without some sample data
0

I worked out your jsFiddle now. There are three ways to do something like you do, depending on what it is exactly that you need to do.

The first way is to create a html table with rows and columns prepared fully, but hide all rows, and only show the rows that you want to show using jQuerys .show() and .hide() depending on the value of the dropdown when the Add button is pressed. <tr id='row373' style='display:none'><td>..</td>..</tr>

The second way is to send all the data for the table using hidden html element like a div containing lis, and to decorate all li with data attributes <li id='row373' data-key='value'>, which can be read by jQuery using the .data('key') function where key is a name for each column of data.

The third, and possibly best way, is to use ajax requests to load single or multiple rows as either .html or .json data. This requires you to write a special .php script that only reads data from the database and spits it out in a format that you like to access with jQuery. The script can be called by jQuerys .ajax() .get() or .post() functions.

2 Comments

Much Appreciated suggestion,but can you help me out through it if i decided to go with the third option? i lack knowledge in ajax.
Perhaps you could try to work out the starters to a solution involving ajax by looking at the documentation, api.jquery.com/jQuery.get . The examples there would work with an ajax handler written in PHP that is returning HTML. The returned html can then be placed into a div using jQuerys html() function with the data of the Ajax response a argument. I'm not sure myself whether it is easy to copy rows from Ajax response data into an already existing table object (in contrast to placing a complete table into a div, which is much easier.). I would suggest you ask a new question then.
0

@Tuna Fish : i used the logic of your code. Guys,thanks a lot of all the help,but i managed to find out a good way of dealing with the issue. you can check the following code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.