CSV File With PHP and MySQL - Data not uploading properly in database - php
I download the script from - http://www.k-fez.com/?p=101 website and trying to upload CSV file on my server. It uploading file successfully but Blank - please check image
<?PHP
session_start();
if($_FILES["file"]["type"] != "application/vnd.ms-excel"){
die("This is not a CSV file.");
}
elseif(is_uploaded_file($_FILES['file']['tmp_name'])){
//Connect to the database
$dbhost = 'Localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'auction_tbl';
$link = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql server');
mysql_select_db($dbname);
//Process the CSV file
$findings = "
<form method=\"post\" action=\"importCSV.php\">
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td>Checking...</td>
";
$handle = fopen($_FILES['file']['tmp_name'], "r");
$data = fgetcsv($handle, 10000, ";"); //Remove if CSV file does not have column headings
while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
$auction_id = mysql_real_escape_string($data[0]);
$name = mysql_real_escape_string($data[1]);
$description = mysql_real_escape_string($data[2]);
$owner_id = mysql_real_escape_string($data[3]);
$list_in = mysql_real_escape_string($data[4]);
$auction_type = mysql_real_escape_string($data[5]);
$start_time = mysql_real_escape_string($data[6]);
$start_time_type = mysql_real_escape_string($data[7]);
$end_time = mysql_real_escape_string($data[8]);
$end_time_type = mysql_real_escape_string($data[9]);
$duration = mysql_real_escape_string($data[10]);
$quantity = mysql_real_escape_string($data[11]);
$category_id = mysql_real_escape_string($data[12]);
$addl_category_id = mysql_real_escape_string($data[13]);
$currency = mysql_real_escape_string($data[14]);
$start_price = mysql_real_escape_string($data[15]);
$reserve_price = mysql_real_escape_string($data[16]);
$buyout_price = mysql_real_escape_string($data[17]);
$is_offer = mysql_real_escape_string($data[18]);
$offer_min = mysql_real_escape_string($data[19]);
$offer_max = mysql_real_escape_string($data[20]);
$enable_swap = mysql_real_escape_string($data[21]);
$bid_increment_amount = mysql_real_escape_string($data[22]);
$zip_code = mysql_real_escape_string($data[23]);
$state = mysql_real_escape_string($data[24]);
$country = mysql_real_escape_string($data[25]);
$postage_amount = mysql_real_escape_string($data[26]);
$insurance_amount = mysql_real_escape_string($data[27]);
$item_weight = mysql_real_escape_string($data[28]);
$shipping_method = mysql_real_escape_string($data[29]);
$shipping_int = mysql_real_escape_string($data[30]);
$type_service = mysql_real_escape_string($data[31]);
$shipping_details = mysql_real_escape_string($data[32]);
$payment_methods = mysql_real_escape_string($data[33]);
$direct_payment = mysql_real_escape_string($data[34]);
$hpfeat = mysql_real_escape_string($data[35]);
$catfeat = mysql_real_escape_string($data[36]);
$bold = mysql_real_escape_string($data[37]);
$hl = mysql_real_escape_string($data[38]);
$hidden_bidding = mysql_real_escape_string($data[39]);
$apply_tax = mysql_real_escape_string($data[40]);
$auto_relist_bids = mysql_real_escape_string($data[41]);
$auto_relist_nb = mysql_real_escape_string($data[42]);
$force_payment = mysql_real_escape_string($data[43]);
$fb_decrement_amount = mysql_real_escape_string($data[44]);
$fb_decrement_interval = mysql_real_escape_string($data[45]);
$fb_next_decrement = mysql_real_escape_string($data[46]);
$images_details = mysql_real_escape_string($data[47]);
$media_details = mysql_real_escape_string($data[48]);
$dd_details = mysql_real_escape_string($data[49]);
$custom_fields_details = mysql_real_escape_string($data[50]);
$import_date = mysql_real_escape_string($data[51]);
//Check if row is in database already
$sql = "SELECT *
FROM `bulk_listings`
WHERE `auction_id` = '" . $auction_id . "'"; //In this example attribute0 is the primary key
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if($count > 0){
$findings = $findings . "
<tr>
<td bgcolor=\"#FF0000\">DB Duplicate</td>
<td>" . $auction_id . "</td>
<td>" . $name . "</td>
<td>" . $description . "</td>
<td>" . $owner_id . "</td>
<td>" . $list_in . "</td>
<td>" . $auction_type . "</td>
<td>" . $start_time . "</td>
<td>" . $start_time_type . "</td>
<td>" . $end_time . "</td>
<td>" . $end_time_type . "</td>
<td>" . $duration . "</td>
<td>" . $quantity . "</td>
<td>" . $category_id . "</td>
<td>" . $addl_category_id . "</td>
<td>" . $currency . "</td>
<td>" . $start_price . "</td>
<td>" . $reserve_price . "</td>
<td>" . $buyout_price . "</td>
<td>" . $is_offer . "</td>
<td>" . $offer_min . "</td>
<td>" . $offer_max . "</td>
<td>" . $enable_swap . "</td>
<td>" . $bid_increment_amount . "</td>
<td>" . $zip_code . "</td>
<td>" . $state . "</td>
<td>" . $country . "</td>
<td>" . $postage_amount . "</td>
<td>" . $insurance_amount . "</td>
<td>" . $item_weight . "</td>
<td>" . $shipping_method . "</td>
<td>" . $shipping_int . "</td>
<td>" . $type_service . "</td>
<td>" . $shipping_details . "</td>
<td>" . $payment_methods . "</td>
<td>" . $direct_payment . "</td>
<td>" . $hpfeat . "</td>
<td>" . $catfeat . "</td>
<td>" . $bold . "</td>
<td>" . $hl . "</td>
<td>" . $hidden_bidding . "</td>
<td>" . $apply_tax . "</td>
<td>" . $auto_relist_bids . "</td>
<td>" . $auto_relist_nb . "</td>
<td>" . $force_payment . "</td>
<td>" . $fb_decrement_amount . "</td>
<td>" . $fb_decrement_interval . "</td>
<td>" . $fb_next_decrement . "</td>
<td>" . $images_details . "</td>
<td>" . $media_details . "</td>
<td>" . $dd_details . "</td>
<td>" . $custom_fields_details . "</td>
<td>" . $import_date . "</td>
</tr>";
}
//Check if row is already in INSERT queue
elseif(strpos($_SESSION['insert'], "'" . $auction_id . "'") != false){
$findings = $findings . "
<tr>
<td bgcolor=\"#FF0000\">File Duplicate</td>
<td>" . $auction_id . "</td>
<td>" . $name . "</td>
<td>" . $description . "</td>
<td>" . $owner_id . "</td>
<td>" . $list_in . "</td>
<td>" . $auction_type . "</td>
<td>" . $start_time . "</td>
<td>" . $start_time_type . "</td>
<td>" . $end_time . "</td>
<td>" . $end_time_type . "</td>
<td>" . $duration . "</td>
<td>" . $quantity . "</td>
<td>" . $category_id . "</td>
<td>" . $addl_category_id . "</td>
<td>" . $currency . "</td>
<td>" . $start_price . "</td>
<td>" . $reserve_price . "</td>
<td>" . $buyout_price . "</td>
<td>" . $is_offer . "</td>
<td>" . $offer_min . "</td>
<td>" . $offer_max . "</td>
<td>" . $enable_swap . "</td>
<td>" . $bid_increment_amount . "</td>
<td>" . $zip_code . "</td>
<td>" . $state . "</td>
<td>" . $country . "</td>
<td>" . $postage_amount . "</td>
<td>" . $insurance_amount . "</td>
<td>" . $item_weight . "</td>
<td>" . $shipping_method . "</td>
<td>" . $shipping_int . "</td>
<td>" . $type_service . "</td>
<td>" . $shipping_details . "</td>
<td>" . $payment_methods . "</td>
<td>" . $direct_payment . "</td>
<td>" . $hpfeat . "</td>
<td>" . $catfeat . "</td>
<td>" . $bold . "</td>
<td>" . $hl . "</td>
<td>" . $hidden_bidding . "</td>
<td>" . $apply_tax . "</td>
<td>" . $auto_relist_bids . "</td>
<td>" . $auto_relist_nb . "</td>
<td>" . $force_payment . "</td>
<td>" . $fb_decrement_amount . "</td>
<td>" . $fb_decrement_interval . "</td>
<td>" . $fb_next_decrement . "</td>
<td>" . $images_details . "</td>
<td>" . $media_details . "</td>
<td>" . $dd_details . "</td>
<td>" . $custom_fields_details . "</td>
<td>" . $import_date . "</td>
</tr>";
}
//Row is unique
else{
//Add INSERT statement to INSERT queue
$_SESSION['insert'] .= "INSERT INTO `bulk_listings` (
`auction_id`,
`name`,
`description`,
`owner_id`,
`list_in`,
`auction_type`,
`start_time`,
`start_time_type`,
`end_time`,
`end_time_type`,
`duration`,
`quantity`,
`category_id`,
`addl_category_id`,
`currency`,
`start_price`,
`reserve_price`,
`buyout_price`,
`is_offer`,
`offer_min`,
`offer_max`,
`enable_swap`,
`bid_increment_amount`,
`zip_code`,
`state`,
`country`,
`postage_amount`,
`insurance_amount`,
`item_weight`,
`shipping_method`,
`shipping_int`,
`type_service`,
`shipping_details`,
`payment_methods`,
`direct_payment`,
`hpfeat`,
`catfeat`,
`bold`,
`hl`,
`hidden_bidding`,
`apply_tax`,
`auto_relist_bids`,
`auto_relist_nb`,
`force_payment`,
`fb_decrement_amount`,
`fb_decrement_interval`,
`fb_next_decrement`,
`images_details`,
`media_details`,
`dd_details`,
`custom_fields_details`,
`import_date`
)
VALUES ('" . $auction_id . "', '" . $name . "', '" . $description . "', '" . $owner_id . "', '" . $list_in . "', '" . $auction_type . "', '" . $start_time . "', '" . $start_time_type . "', '" . $end_time . "', '" . $end_time_type . "', '" . $duration . "', '" . $quantity . "', '" . $category_id . "', '" . $addl_category_id . "', '" . $currency . "', '" . $start_price . "', '" . $reserve_price . "', '" . $buyout_price . "', '" . $is_offer . "', '" . $offer_min . "', '" . $offer_max . "', '" . $enable_swap . "', '" . $bid_increment_amount . "', '" . $zip_code . "', '" . $state . "', '" . $country . "', '" . $postage_amount . "', '" . $insurance_amount . "', '" . $item_weight . "', '" . $shipping_method . "', '" . $shipping_int . "', '" . $type_service . "', '" . $shipping_details . "', '" . $payment_methods . "', '" . $direct_payment . "', '" . $hpfeat . "', '" . $catfeat . "', '" . $bold . "', '" . $hl . "', '" . $hidden_bidding . "', '" . $apply_tax . "', '" . $auto_relist_bids . "', '" . $auto_relist_nb . "', '" . $force_payment . "', '" . $fb_decrement_amount . "', '" . $fb_decrement_interval . "', '" . $fb_next_decrement . "', '" . $images_details . "', '" . $media_details . "', '" . $dd_details . "', '" . $custom_fields_details . "', '" . $import_date . "');";
//Add row for row to findings table and mark unique
$findings = $findings . "
<tr>
<td bgcolor=\"#00FF00\"> </td>
<td>" . $auction_id . "</td>
<td>" . $name . "</td>
<td>" . $description . "</td>
<td>" . $owner_id . "</td>
<td>" . $list_in . "</td>
<td>" . $auction_type . "</td>
<td>" . $start_time . "</td>
<td>" . $start_time_type . "</td>
<td>" . $end_time . "</td>
<td>" . $end_time_type . "</td>
<td>" . $duration . "</td>
<td>" . $quantity . "</td>
<td>" . $category_id . "</td>
<td>" . $addl_category_id . "</td>
<td>" . $currency . "</td>
<td>" . $start_price . "</td>
<td>" . $reserve_price . "</td>
<td>" . $buyout_price . "</td>
<td>" . $is_offer . "</td>
<td>" . $offer_min . "</td>
<td>" . $offer_max . "</td>
<td>" . $enable_swap . "</td>
<td>" . $bid_increment_amount . "</td>
<td>" . $zip_code . "</td>
<td>" . $state . "</td>
<td>" . $country . "</td>
<td>" . $postage_amount . "</td>
<td>" . $insurance_amount . "</td>
<td>" . $item_weight . "</td>
<td>" . $shipping_method . "</td>
<td>" . $shipping_int . "</td>
<td>" . $type_service . "</td>
<td>" . $shipping_details . "</td>
<td>" . $payment_methods . "</td>
<td>" . $direct_payment . "</td>
<td>" . $hpfeat . "</td>
<td>" . $catfeat . "</td>
<td>" . $bold . "</td>
<td>" . $hl . "</td>
<td>" . $hidden_bidding . "</td>
<td>" . $apply_tax . "</td>
<td>" . $auto_relist_bids . "</td>
<td>" . $auto_relist_nb . "</td>
<td>" . $force_payment . "</td>
<td>" . $fb_decrement_amount . "</td>
<td>" . $fb_decrement_interval . "</td>
<td>" . $fb_next_decrement . "</td>
<td>" . $images_details . "</td>
<td>" . $media_details . "</td>
<td>" . $dd_details . "</td>
<td>" . $custom_fields_details . "</td>
<td>" . $import_date . "</td>
</tr>";
}
}
mysql_close($link);
$findings = $findings . "
<tr>
<td colspan=\"5\"><div align=\"center\"><input type=\"submit\" value=\"Confirm\" /></div></td>
</tr>
</table>
</form>";
echo $findings;
}
else{
die("You shouldn't be here");
}
?>
importCSV.php (Sorry i forgot to not another file code.)
<?PHP
session_start();
if(!isset($_SESSION['insert'])){
die("You shouldn't be here.");
}
//Connect to the database
$dbhost = 'Localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'auction_tbl';
$link = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql server');
mysql_select_db($dbname);
$queries = explode(';', $_SESSION['insert']);
foreach($queries as $query){
if($query != ""){
mysql_query($query);
}
}
mysql_close($link);
echo "Done";
?>
Thanks
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
mysql_query("INSERT INTO contacts_tmp (contact_first, contact_last, contact_email) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
Check Database Field Data type Properly ..
I think database field data type is wrong for this problem..
Related
How can I check if all checkboxes have been checked in PHP?
I am implementing a list of items and each has a checkbox. I currently can see which checkboxes have been checked but what I want to do is check if all of them have been checked. How can I implement that? Here is my code: <form action="" method="post"> <?php echo "<table> <tr> <th>Customer ID</th> <th>Report ID</th> <th>Report message</th> <th>Device</th> <th>Device no.</th> <th>Barcode</th> <th>IMEI</th> <th>Sale-date</th> </tr>"; while ($row2 = $clientUsername->fetch_assoc()) { $_SESSION['cl_username'] = $row2["username"]; while ($row = $message->fetch_assoc()) { $_SESSION['accept'] = $row["acceptance"]; $_SESSION['client_comment'] = $row["message"]; $_SESSION['name'] = $row["name"]; $_SESSION['sales_date'] = $row["sales_date"]; $_SESSION['date_sent'] = $row["date_sent"]; $_SESSION['countable_array'] = $row; ?> <?php if ($row['acceptance'] == 3) { echo "<tr> <td> " . '<input type=checkbox name=devices[] value=' . $row['dev_id'] . '>' . " </td> <td>" . $cus_id . " </td> <td>" . $rep_id . "</td> <td>" . $_SESSION['client_comment'] . "</td> <td>" . $_SESSION['name'] . "</td> <td>" . $row["device_no"] . "</td> <td>" . $row["barcode"] . "</td> <td>" . $row["serial_imei"] . "</td> <td>" . $row["serial_no"] . "</td> <td>" . $row["sales_date"] . "</td></tr>"; echo "</table>"; } } } </form> if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['rejected'])) { if (count($count_devices) == 1) { ... } }
<?php while ($row2 = $clientUsername->fetch_assoc()) { $_SESSION['cl_username'] = $row2["username"]; $i = 0; // initiate the variable here $total_number_of_rows = $message->num_rows(); // Get total number of rows from your object while ($row = $message->fetch_assoc()) { $_SESSION['accept'] = $row["acceptance"]; $_SESSION['client_comment'] = $row["message"]; $_SESSION['name'] = $row["name"]; $_SESSION['sales_date'] = $row["sales_date"]; $_SESSION['date_sent'] = $row["date_sent"]; $_SESSION['countable_array'] = $row; if ($row['acceptance'] == 3) { $i++; // When conditions trues, increment the variable. echo "<tr> <td> " . '<input type=checkbox name=devices[] value=' . $row['dev_id'] . '>' . " </td> <td>" . $cus_id . " </td> <td>" . $rep_id . "</td> <td>" . $_SESSION['client_comment'] . "</td> <td>" . $_SESSION['name'] . "</td> <td>" . $row["device_no"] . "</td> <td>" . $row["barcode"] . "</td> <td>" . $row["serial_imei"] . "</td> <td>" . $row["serial_no"] . "</td> <td>" . $row["sales_date"] . "</td></tr>"; echo "</table>"; } } if($i == $total_number_of_rows){ // Here implement this condition, If both equal then all inputs have checked. echo "Check box checked all inputs"; } } ?> I think you expecting the same as above. We need to check the Total number of rows with Incremental variable value. Please review my comment inside the code part, so that you can understand terms.
where to enter " " in the following code?
Can some one kindly help me find a place to add   between each of the rows in the echo statement. where and how can I add blank space between each row? kindly ignore the first set of echo statements that have been commented out. Thank you... <link rel="stylesheet" href="docfullcss.css"> <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $dbname = 'employee_info'; $conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); if(! $conn ) { die('Could not connect: ' . mysqli_connect_error()); } $sql = 'SELECT * FROM `docfull` ORDER BY `COL 7` ASC'; $retval = mysqli_query($conn,$sql); if(! $retval ) { die('Could not get data: ' . mysqli_error($conn)); } while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) { /* (ignore this commented section) echo "<table>"; echo "<tr>"; echo "<td>COL 1</td>"; echo "<td>COL 2</td>"; echo "</tr>"; echo "</table>"; */ echo "<tr><td>" . $row['COL 1'] . "</td> <td>" . $row['COL 2'] . "</td> </tr>" . $row['COL 3'] . "</td> <td>" . $row['COL 4'] . "</td> <td>" . $row['COL 5'] . "</td> <td>" . $row['COL 6'] . "</td> <td>" . $row['COL 7'] . "</td> <td>" . $row['COL 8'] . "</td> <td>" . $row['COL 9'] . "</td> <td>" . $row['COL 10'] . "</td> <td>" . $row['COL 11'] . "</td> <td>" . $row['COL 12'] . "</td> <td>" . $row['COL 13'] . "</td> <td>" . $row['COL 14'] . "</td> <td>" . $row['COL 15'] . "</td> <td>" . $row['COL 16'] . "</td><td>"; echo "<br><br>"; } mysqli_close($conn); ?>
Problem is here echo "<tr><td>" . $row['COL 1'] . "</td><td>" . $row['COL 2'] . "</td></tr>" . $row['COL 3'] . You end row after COL 2 and never start again till another iteration. You need to remove this </tr> after COL 2 and add <td> to this place and add </tr> after . $row['COL 16'] . "</td><td>"; , also you don'd need last td and those <br> tags on next line. To have simple table you need something like this: echo '<table>'; while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) { echo "<tr><td>" . $row['COL 1'] . "</td> <td>" . $row['COL 2'] . "</td> <td>" . $row['COL 3'] . "</td> <td>" . $row['COL 4'] . "</td> <td>" . $row['COL 5'] . "</td> <td>" . $row['COL 6'] . "</td> <td>" . $row['COL 7'] . "</td> <td>" . $row['COL 8'] . "</td> <td>" . $row['COL 9'] . "</td> <td>" . $row['COL 10'] . "</td> <td>" . $row['COL 11'] . "</td> <td>" . $row['COL 12'] . "</td> <td>" . $row['COL 13'] . "</td> <td>" . $row['COL 14'] . "</td> <td>" . $row['COL 15'] . "</td> <td>" . $row['COL 16'] . "</td></tr>"; } echo '</table>';
Add button in a table retrieved from database with php
I am able to get all the content of my table from the database using php. How can I add a button in one of the column of the table , or either another component like a select or a checkbox.... I post a pic to make it more clear. I get the table using php but how to insert the component in the row? $db = new mysqli("...", "...", "...", "..."); if($db->connect_errno > 0){ die('Unable to connect to database [' . $db->connect_error . ']'); } $sql = "SELECT * from ..."; if(!$result = $db->query($sql)){ die('There was an error running the query [' . $db->error . ']'); } echo " <table class='table'> <thead> <tr>"; /* Get field information for all columns */ while ($finfo = $result->fetch_field()) { echo " <th>" . $finfo->name . "</th>"; } echo " </tr> </thead> <tbody>"; while($row = $result->fetch_assoc()){ echo "<tr class='info'> <td>" . $row['id'] . "</td> <td>" . $row['name'] . "</td> <td>" . $row['mail'] . "</td> <td>" . $row['number'] . "</td> <td>" . $row['device'] . "</td> <td>" . $row['price'] . "</td> <td>" . $row['paymenttype'] . "</td> <td>" . $row['status'] . "</td> <td> add </td> </tr>"; } echo " </tbody> </table>"; ?> -----------------update echo "<tr class='info'> <td>" . $row['id'] . "</td> <td>" . $row['name'] . "</td> <td>" . $row['mail'] . "</td> <td>" . $row['number'] . "</td> <td>" . $row['device'] . "</td> <td>" . $row['price'] . "</td> <td>" . $row['paymenttype'] . "</td> <td>" . $row['status'] . "</td> <td><input type='checkbox'></td> <td><a href='http://yourlink' >delete</a></td> </tr>";
<table class='table'> <thead> <tr>"; /* Get field information for all columns */ while ($finfo = $result->fetch_field()) { echo " <th>" . $finfo->name . "</th>"; } echo " </tr> </thead> <tbody>"; while($row = $result->fetch_assoc()){ echo "<tr class='info'> <td>" . $row['id'] . "</td> <td>" . $row['name'] . "</td> <td>" . $row['mail'] . "</td> <td>" . $row['number'] . "</td> <td>" . $row['device'] . "</td> <td>" . $row['price'] . "</td> <td>" . $row['paymenttype'] . "</td> <td>" . $row['status'] . "</td> <td> <button class='btn' >Button</button> </td> </tr>"; } echo " </tbody> </table>"; ?>
Found this solution <td><a class='btn btn-primary btn-lg' href='send.php?name=".$row['name']."'>Send</a></td>
I want to upload a file and when the file upload, the values of the file has to be put in my array
Below you can see my formpage, at this page you upload a xml file. <html> <body> <form enctype="multipart/form-data" action="arrayincludefiletest.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000" /> <table width="600"> <tr> <td>File name:</td> <td><input type="file" name="file" /></td> <td><input type="submit" value="Upload" /></td> </tr> </table> </form> </body> </html> Below you can see my PHP code, if you take a look you can see that now everything is manual. I have to fill the array by myself. You can also see that I insert the array into my database. I want to upload a xml file and that the values of the xml file automatic be put in my array. <html> <head> <title> Bom Array </title> </head> <body> <?php $bom= array( array("Aantal" =>1, "Manufactorer" =>"Panasonic", "Partno" =>"EEEFC1H1R0R", "Description" =>"Capacitor 0603","Footprint" =>"CAP0603", "Refdes" =>"B1"), array("Aantal" =>2, "Manufactorer" =>"Vishay", "Partno" =>"MAL215371228E3", "Description" =>"Capacitor 1210","Footprint" =>"CAP1210", "Refdes" =>"C6,C7"), array("Aantal" =>3, "Manufactorer" =>"Ferroxcube", "Partno" =>"MAL215371109E3", "Description" =>"Buzzer 80dB 3,4 KHz","Footprint" =>"KPEG238", "Refdes" =>"C8,C25"), array("Aantal" =>4, "Manufactorer" =>"Philips", "Partno" =>"EEEFC1E101P", "Description" =>"Tantaal 100uF, 6,3V Case_B","Footprint" =>"Case_B", "Refdes" =>"C1") ); echo "<table border='1'>"; echo "<tr> <th>Aantal</th> <th>Manufactorer</th> <th>Partno</th> <th>Description</th> <th>Footprint</th> <th>Refdes</th> </tr>"; echo "<tr> <td>" . $bom[0]['Aantal'] . "</td> <td>" . $bom[0]['Manufactorer'] . "</td> <td>" . $bom[0]['Partno'] . "</td> <td>" . $bom[0]['Description'] . "</td> <td>" . $bom[0]['Footprint'] . "</td> <td>" . $bom[0]['Refdes'] . "</td> </tr>"; echo "<tr> <td>" . $bom[1]['Aantal'] . "</td> <td>" . $bom[1]['Manufactorer'] . "</td> <td>" . $bom[1]['Partno'] . "</td> <td>" . $bom[1]['Description'] . "</td> <td>" . $bom[1]['Footprint'] . "</td> <td>" . $bom[1]['Refdes'] . "</td> </tr>"; echo "<tr> <td>" . $bom[2]['Aantal'] . "</td> <td>" . $bom[2]['Manufactorer'] . "</td> <td>" . $bom[2]['Partno'] . "</td> <td>" . $bom[2]['Description'] . "</td> <td>" . $bom[2]['Footprint'] . "</td> <td>" . $bom[2]['Refdes'] . "</td> </tr>"; echo "<tr> <td>" . $bom[3]['Aantal'] . "</td> <td>" . $bom[3]['Manufactorer'] . "</td> <td>" . $bom[3]['Partno'] . "</td> <td>" . $bom[3]['Description'] . "</td> <td>" . $bom[3]['Footprint'] . "</td> <td>" . $bom[3]['Refdes'] . "</td> </tr>"; echo "</table>"; // Connectie database and Insert into database $con = mysqli_connect("localhost", "csa", "csa", "csa"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL:" . mysqli_connect_error(); } if(is_array($bom)) { $sql= "INSERT INTO bom (Aantal, Manufactorer, Partno, Description, Footprint, Refdes) values"; $valuesArr = array(); foreach ($bom as $row) { $aantal = (int) $row['Aantal']; $manufactorer = mysqli_real_escape_string($con, $row['Manufactorer']); $partno = mysqli_real_escape_string($con, $row['Partno']); $description = mysqli_real_escape_string($con, $row['Description']); $footprint = mysqli_real_escape_string($con, $row['Footprint']); $refdes = mysqli_real_escape_string($con, $row['Refdes']); $valuesArr[] = "('$aantal', '$manufactorer', '$partno', '$description', '$footprint', '$refdes')"; } $sql .= implode(',', $valuesArr); mysqli_query($con, $sql) or die('Error:' . mysqli_errno($con)); } mysqli_close($con); ?> </body> </html>
Use xml_parse_into_struct() function for this. $xmlfile = 'test.xml'; $xmlparser = xml_parser_create(); $fp = fopen($xmlfile, 'r'); $xmldata = fread($fp, 4096); xml_parse_into_struct($xmlparser,$xmldata,$values); xml_parser_free($xmlparser); print_r($values);
Creating a function that contains a while loop
I am new to PHP and trying to learn it by creating a database of all my jobs (I'm a freelance designer). I have created the code below to generate a table to display all jobs where the description contains logo which works fine and generates several rows.... <?php $logojobs = mysql_query("SELECT * FROM hlgd_projects WHERE description LIKE '%logo%'", $connection); ?> View logo jobs <?php if ($report == 'logo') { echo "<h1>Logo jobs</h1>"; echo "<table id='report'> <tr> <th></th> <th>Status</th> <th>Lead</th> <th>Start</th> <th colspan='2'>Codes</th> <th>Client</th> <th>Description</th> <th>Fee</th> <th>Contact</th> <th colspan='2'>Invoice</th> <th>Paid</th> </tr>"; while ($logojob = mysql_fetch_array($logojobs)) { echo "<tr> <td class='id'>" . $logojob['id'] . "</td> <td>" . $logojob['status_id'] . "</td> <td>" . $logojob['lead_id'] . "</td> <td>" . $logojob['date_start'] . "</td> <td>" . $logojob['code_lead'] . "</td> <td>" . $logojob['code_hlgd'] . "</td> <td>" . $logojob['client'] . "</td> <td>" . $logojob['description'] . "</td> <td>£" . $logojob['fee'] . "</td> <td>" . $logojob['contact'] . "</td> <td>" . $logojob['invoice'] . "</td> <td>" . $logojob['date_inv'] . "</td> <td>" . $logojob['date_paid'] . "</td> </tr>"; } echo "</table>"; } ?> However I would like to be able to generate reports for lots of different things and so would like to create a function to generate the table and pass the relevant arguments each time. I've done this as follows but it only generates the first row so I presume it's ignoring the while? Can anyone tell me where I'm going wrong or how to better put the code above into a function? <?php function report_bytype($title,$job_set,$job_name) { $reporthead = "<h1>$title</h1> <table id='report'> <tr> <th></th> <th>Status</th> <th>Lead</th> <th>Start</th> <th colspan='2'>Codes</th> <th>Client</th> <th>Description</th> <th>Fee</th> <th>Contact</th> <th colspan='2'>Invoice</th> <th>Paid</th> </tr>"; while ($job_name = mysql_fetch_array($job_set)) { $reportrows = " <tr> <td class='id'>" . $job_name['id'] . "</td> <td>" . $job_name['status_id'] . "</td> <td>" . $job_name['lead_id'] . "</td> <td>" . $job_name['date_start'] . "</td> <td>" . $job_name['code_lead'] . "</td> <td>" . $job_name['code_hlgd'] . "</td> <td>" . $job_name['client'] . "</td> <td>" . $job_name['description'] . "</td> <td>£" . $job_name['fee'] . "</td> <td>" . $job_name['contact'] . "</td> <td>" . $job_name['invoice'] . "</td> <td>" . $job_name['date_inv'] . "</td> <td>" . $job_name['date_paid'] . "</td> </tr>"; } $reportfoot = "</table>"; $reporttable = $reporthead . $reportrows . $reportfoot; echo $reporttable; return $reporttable; } ?> <?php if($report == 'logo') { report_bytype("logo",$logojobs,$logojob); } if($report == 'stationery') { report_bytype("stationery",$stationeryjobs,$stationeryjob); } ?> Many thanks in advance, Helen
The problem is this line: while ($job_name = mysql_fetch_array($job_set)) { $reportrows = "lots of html" } Every time this loops $reportrows is set to the html for that row only. Use $reportrows .= "some html"; instead which will add each row to $reportrows rather than replace $reportrows with that row. Edit: Replacing += with .=.
I can see why you have errors you are not concatenating your result Please see http://php.net/manual/en/language.operators.string.php for more detailed explanations Replace $reportrows = " With $reportrows .= " Full Script function report_bytype($title, $job_set, $job_name) { $reporthead = "<h1>$title</h1> <table id='report'> <tr> <th></th> <th>Status</th> <th>Lead</th> <th>Start</th> <th colspan='2'>Codes</th> <th>Client</th> <th>Description</th> <th>Fee</th> <th>Contact</th> <th colspan='2'>Invoice</th> <th>Paid</th> </tr>"; $reportrows = ""; while ( $job_name = mysql_fetch_array ( $job_set ) ) { $reportrows .= " <tr> <td class='id'>" . $job_name ['id'] . "</td> <td>" . $job_name ['status_id'] . "</td> <td>" . $job_name ['lead_id'] . "</td> <td>" . $job_name ['date_start'] . "</td> <td>" . $job_name ['code_lead'] . "</td> <td>" . $job_name ['code_hlgd'] . "</td> <td>" . $job_name ['client'] . "</td> <td>" . $job_name ['description'] . "</td> <td>£" . $job_name ['fee'] . "</td> <td>" . $job_name ['contact'] . "</td> <td>" . $job_name ['invoice'] . "</td> <td>" . $job_name ['date_inv'] . "</td> <td>" . $job_name ['date_paid'] . "</td> </tr>"; } $reportfoot = "</table>"; $reporttable = $reporthead . $reportrows . $reportfoot; echo $reporttable; return $reporttable; } Thanks