Jump to content

mcfcben

New Members
  • Posts

    3
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

mcfcben's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. been tweaking this more, and now ive got it working with csv a bit better my main problem is that preg_replace refuses to recognise that im trying to pass a table field to be edited, not a string (it strips c.products_model of the . and _ which is great as it shows the preg_replace actually works... but i want c.products_model to be called and the data within this field to be stripped... :/) its probably a really basic fix, but im stumped!
  2. Hi guys, I'm currently working on a piece of code, where I'm aiming to strip special characters such as '.' and '-', leaving just numbers and letters format, this data needs to be exported to a csv file and held there. I've researched preg_replace and decided to use it, and found that the code I created did work when random text/characters was used in the $input array and an echo on the page was tested $input .="c.products_model"; $output = preg_replace("/[^a-zA-Z0-9\s]/","",$input); (in the above code, c.products_model is relating to the database field I need to call and modify, then be shown in the csv file with special characters stripped out) the SQL query I'm using to export the data to csv is structured like this: - i dont think this is where the issue lies but thought i'd include it just in case $sql_query = "SELECT concat( '" . $productURL . "' ,b.products_id) AS product_url, a.manufacturers_name, c.products_model, $output AS products_model_2 from $table WHERE a.manufacturers_id = c.manufacturers_id AND b.products_id = c.products_id AND $wherecond ORDER BY $ordersort"; Where am I going wrong here? I've tried a few workarounds and I'm postive the issue is regarding $input as the actual preg_replace code ive called does work when i change the input variable
  3. Hi guys, I'm having a weird problem relating to the exportation of data to a .csv file. Basically, I need to export specific fields from three different tables into one sheet. This process runs fine and I have successfully extracted the data needed and it shows up in excel. I'm using two files for this: export_products_feed.php (this is basically where my mysql connections and table variable calling is kept) <?php require('includes/application_top.php'); require('exportcsv.inc.php'); $table="manufacturers a, products_description b, products c"; exportMysqlToCsv($table); ?> The second page being used is where the problem lies: exportcsv.inc.php <?php function exportMysqlToCsv($table,$filename = 'products_stock.csv') { $ordersort .="manufacturers_name, products_name ASC"; $wherecond .= "c.products_quantity >='1'"; $csv_terminated = "\n"; $csv_separator = ","; $csv_enclosed = '"'; $csv_escaped = "\\"; $sql_query = "select a.manufacturers_name, b.products_name, b.products_url, c.products_quantity from $table WHERE a.manufacturers_id = c.manufacturers_id AND b.products_id = c.products_id AND $wherecond ORDER BY $ordersort" ; $result = mysql_query($sql_query); $fields_cnt = mysql_num_fields($result); $schema_insert = ''; for ($i = 0; $i < $fields_cnt; $i++) { $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(mysql_field_name($result, $i))) . $csv_enclosed; $schema_insert .= $l; $schema_insert .= $csv_separator; } // end for $out = trim(substr($schema_insert, 0, -1)); $out .= $csv_terminated; // Format the data while ($row = mysql_fetch_array($result)) { $schema_insert = ''; for ($j = 0; $j < $fields_cnt; $j++) { if ($row[$j] == '0' || $row[$j] != '') { if ($csv_enclosed == '') { $schema_insert .= $row[$j]; } else { $schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed; } } else { $schema_insert .= ''; } if ($j < $fields_cnt - 1) { $schema_insert .= $csv_separator; } } // end for $out .= $schema_insert; $out .= $csv_terminated; } // end while header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Length: " . strlen($out)); // Output to browser with appropriate mime type, you choose header("Content-type: text/x-csv"); //header("Content-type: text/csv"); //header("Content-type: application/csv"); header("Content-Disposition: attachment; filename=$filename"); echo $out; exit; } ?> The area of the code that I'm having issues with is: $ordersort .="manufacturers_name, products_name ASC"; $wherecond .= "c.products_quantity >='1'"; $csv_terminated = "\n"; $csv_separator = ","; $csv_enclosed = '"'; $csv_escaped = "\\"; $sql_query = "select a.manufacturers_name, b.products_name, b.products_url, c.products_quantity from $table WHERE a.manufacturers_id = c.manufacturers_id AND b.products_id = c.products_id AND $wherecond ORDER BY $ordersort" ; Now, this is where my slight problem is... when I error tested $sql_query to see why products_url fields were not showing up, i found that the code would work fine and all the field columns in excel would contain the selected data until the b.products_id = c.products_id section was added in (this just basically means that the products dont appear more than once in excel) I've user tested other ways, and either, every field column gets data inserted into it, including products_url, but the output is skewed, or the output is correct, but the products_url field (which is contained in products_description table) stays blank. Any ideas where I'm going wrong with this? As the products_url field needs to output the URL of the product for a feed/report for work. Cheers
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.