-
Posts
24,573 -
Joined
-
Last visited
-
Days Won
824
Everything posted by Barand
-
how i can i let user to download a data from query in mySQL
Barand replied to VanillaRose's topic in MySQL Help
File 1 <a href="file2.php">Download newfies</a> File2.php (the download code goes in a file of its own) <?php $sql = 'SELECT * FROM newfies'; sql2csv($db_mysqli, $sql, $filename='myNewfile.csv', $headings=0); function sql2csv($db_mysqli, $sql, $filename='', $headings=0) { if (!$filename) $f = 'download_' . date('ymdhi') . '.csv'; else $f = $filename; $fp = fopen('php://output', 'w'); // so you can fputcsv to STDOUT if ($fp) { $res = $db_mysqli->query($sql); if ($res) { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="'.$f.'"'); header('Pragma: no-cache'); header('Expires: 0'); $row = $res->fetch_assoc(); if ($headings) { fputcsv($fp, array_keys($row),'|'); } do { fputcsv($fp, $row); } while ($row = $res->fetch_assoc()); } else echo "Error in query"; fclose($fp); } } ?> -
You don't show your call to the function. Is this code in a file of its own?
-
This methods creates and downloads in a single operation http://forums.phpfreaks.com/topic/296581-how-i-can-i-let-user-to-download-a-data-from-query-in-mysql/?do=findComment&comment=1512967
-
I used MySqli because that's what I have, but it should be easily converted for sqlsrv. Here's the method /***************************************************** * first you need to get the column headings * and create array to store responses for * each question ******************************************************/ $sql = "SELECT DISTINCT name FROM kelvin ORDER BY name"; $names = array(); $res = $db->query($sql); while ($row = $res->fetch_assoc()) { $names[] = $row['name']; } $tableHeads = "<tr><th>Question</th><th>" . join('</th><th>', $names) . "</th></tr>\n"; $newArray = array_fill_keys($names,''); // create blank array /***************************************************** * then you process the table data * storing the answers for each name in the array. * * output the array for each question when the question * value changes, then start with a new array for the new * question ******************************************************/ $sql = "SELECT question, name, answer FROM kelvin ORDER BY question"; $qarray = $newArray; // new array to store answers to question $currq = ''; // store the current question $tableData = ''; $res = $db->query($sql); while ($row = $res->fetch_assoc()) { if ($row['question'] != $currq) { // change of question? if ($currq) { // have we a question yet? $tableData .= "<tr><td>$currq</td><td>"; $tableData .= join('</td><td>', $qarray) . "</td></tr>\n"; } $currq = $row['question']; // store new question $qarray = $newArray; // reset the array } $qarray[$row['name']] = $row['answer']; // store the answer by name } // output the stored array for the final question $tableData .= "<tr><td>$currq</td><td>"; $tableData .= join('</td><td>', $qarray) . "</td></tr>\n"; ?> <table border='1'> <?=$tableHeads?> <?=$tableData?> </table> My data mysql> SELECT * FROM test.kelvin; +----+---------+------------+--------------------+--------------------+ | id | name | questionid | question | answer | +----+---------+------------+--------------------+--------------------+ | 1 | Foo | 1000 | How are you? | I'm Fine | | 2 | Foo | 1001 | What is Your name? | My Name is Foo | | 3 | Ben | 1000 | How are you? | I Feel sick | | 4 | Ben | 1001 | What is Your name? | My name is Ben | | 5 | Charlie | 1001 | What is Your name? | My name is Charlie | +----+---------+------------+--------------------+--------------------+ Results attached
-
The connection is to the server so if the databases are on the same server then a single connection is sufficient. You can, if you want, mix databases in a single query if on the same server SELECT ... FROM database1.tableA as a INNER JOIN database2.tableB as b
-
Also note the the use of session_register() was replaced by $_SESSION in December 2001.
-
How to select two tables and compare two columns and determine < or =
Barand replied to jgreen's topic in PHP Coding Help
Of course it's for the select box - that's what you said your problem was -
Perhaps function priceFormat ($val, $currency) { $vi = intval($val); $dec = $currency=='€' ? ',' : '.'; if ($val == $vi) { return "$currency$vi"; } else { return sprintf('%s%d%s<small>%02.0f</small>', $currency,$vi,$dec,($val-$vi)*100); } } echo priceFormat(10.00, '£') . '<br>'; echo priceFormat(10.99, '£') . '<br>'; echo priceFormat(120.00, '€') . '<br>'; echo priceFormat(120.99, '€') . '<br>';
-
How to select two tables and compare two columns and determine < or =
Barand replied to jgreen's topic in PHP Coding Help
data user_info books +--------------+----------+-------+ +----+-------+--------+ | user_info_id | username | title | | id | title | copies | +--------------+----------+-------+ +----+-------+--------+ | 1 | qqq | aaa | | 1 | aaa | 2 | | 2 | www | aaa | | 2 | bbb | 3 | | 3 | eee | bbb | | 3 | ccc | 2 | | 4 | fff | ccc | | 4 | ddd | 3 | | 5 | ggg | ccc | +----+-------+--------+ | 6 | hhh | ddd | +--------------+----------+-------+ then $result = mysqli_query($con,"SELECT b.id , b.title , b.copies FROM books b LEFT JOIN user_info u USING (title) GROUP BY b.id HAVING COUNT(u.title) < b.copies"); while($row = mysqli_fetch_array($result)) { echo ("<option value='$row[title]'>$row[title]</option>"); } generated HTML <select name="books"> <option value="">Select a Book</option> <option value="bbb">bbb</option> <option value="ddd">ddd</option> </select> -
I know. I usually use !feof() and was confusing myself.
-
nm!
-
Replace $buffer with $stream
-
// pow($x, 3) == 1200/3; // therefore $x = pow(1200/3 , 1/3); echo $x; //--> 7.3680629972808 (not 240, but close) to confirm echo 3 * 7.3680629972808 * 7.3680629972808 * 7.3680629972808; // --> 1200
-
Simple arithmetic // select the next perPage records $sql = "SELECT restaurant_name FROM restaurant ORDER BY restaurant_name LIMIT $offset, $perPage"; $res = $db->query($sql); $recs = $res->num_rows; $p = ($page - 1) * $perPage + 1; $q = $p + $recs - 1; $output = "Showing items $p to $q of $numrecs<br><br>";
-
Even if PHP is supported then changing the file names from .php to .html will prevent any php code from executing unless the server is reconfigured.
-
mysqli prepared statement mysqli_fetch_array()
Barand replied to aHMAD_SQaLli's topic in PHP Coding Help
Apologies for not being online when you posted. Had I known you were in such a hurry I would have sat in front of my screen all day just waiting for you to post a problem. -
Here's an example <?php session_start(); include("db_inc.php"); // defines HOST, USERNAME and PASSWORD try { $db = new mysqli(HOST,USERNAME,PASSWORD,'test'); } catch(Exception $e) { die("DB connection error"); } $perPage = 30; // set how many records to display on each page if (!isset($_SESSION['pages'])) { // count records to determine how many pages $sql = "SELECT COUNT(*) FROM restaurant"; $res = $db->query($sql); list($numrecs) = $res->fetch_row(); $_SESSION['pages'] = ceil($numrecs/$perPage); } $totalPages = $_SESSION['pages']; // get current page number $page = isset($_GET['page']) ? $_GET['page'] : 1; // calculate offset for query LIMIT clause $offset = ($page - 1) * $perPage; // select the next perPage records $sql = "SELECT restaurant_name FROM restaurant ORDER BY restaurant_name LIMIT $offset, $perPage"; $res = $db->query($sql); $output = ''; while ($row = $res->fetch_assoc()) { $output .= $row['restaurant_name'] . '<br>'; } function pageNav ($page, $total) { $str = 'Pages '; if ($total < 11) { for ($p=1; $p <= $total; $p++) { $class = $page==$p ? 'nolink' : 'link'; $str .= "<div class='nav $class'>$p</div>"; } } else { $class = $page==1 ? 'nolink' : 'link'; $str .= "<div class='nav $class'>1</div>"; if ($page < 5) { $p1 = 2; $p2 = 6; } else { $p1 = min ($page-2, $total-5); $p2 = min ($page+2, $total); $str .= '... '; } for ($p=$p1; $p <= $p2; $p++) { $class = $page==$p ? 'nolink' : 'link'; $str .= "<div class='nav $class'>$p</div>"; } if ($total-$page > 3) $str .= '... '; $class = $page==$total ? 'nolink' : 'link'; if ($total > $page+2) $str .= "<div class='nav $class'>$total</div>"; } return $str; } ?> <!DOCTYPE html> <html> <head> <title>Pagination Example</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript"> $().ready(function() { $(".link").click(function() { var pgnum = $(this).html(); location.href = "?page="+pgnum; // specify action on click of page number here }) }) </script> <style type="text/css"> div.nav { display: inline-block; width: 16px; height: 16px; margin-right: 5px; padding: 0; text-align: center; font-family: sans-serif; font-size: 9pt; } div.link { border: 1px solid blue; cursor: pointer; } div.nolink { font-weight: 700; border: none; } div#rests { font-family: sans-serif; font-size: 12pt; padding: 20px; color: #AA16B6; width: 400px; margin-top: 20px; margin-bottom: 20px; margin-left: auto; margin-right: auto; border-top: 1px solid gray; border-bottom: 1px solid gray; } div.pagenums { text-align: center; } </style> </head> <body> <h1>Restaurants</h1> <div class='pagenums'> <?=pageNav($page, $totalPages)?> </div> <div id='rests'> <?=$output?> </div> <div class='pagenums'> <?=pageNav($page, $totalPages)?> </div> </body> </html>
-
Find a User not in another table for specified dates
Barand replied to jbradley04's topic in MySQL Help
Joins are usually more efficient SELECT * FROM USER u LEFT JOIN stories s ON u.ID = s.CBy AND s.con BETWEEN '2014-07' AND '2015-07' WHERE s.CBy IS NULL BTW, you shouldn't have had the comma at the end of your first line -
How to select two tables and compare two columns and determine < or =
Barand replied to jgreen's topic in PHP Coding Help
try this SELECT b.id , b.title , b.copies FROM books b LEFT JOIN user_info u USING (title) GROUP BY b.id HAVING COUNT(u.title) < b.copies -
Correction to my code above - should be echo '<pre>', print_r($output, true), '</pre>';
-
String values in a query need to be inside single quotes $sql = "UPDATE `hsa_project_hub`.`rfis` SET `answered_by` = '$answeredBy', `time` = NOW() WHERE `rfis`.`no` = 23;";
-
How to select two tables and compare two columns and determine < or =
Barand replied to jgreen's topic in PHP Coding Help
I see no title column in user_info table ??? -
Personally I prefer to use print_r() and only use var_dump() when I need to check type and size and see if there is any hidden whitespace or non-printable characters. When using either, use between <pre>..</pre> tags. It makes it much more readable. eg $output= json_decode($geocode); echo '<pre>', print_r($output), '</pre>';
-
Submit $_POST with dynamically generated fields
Barand replied to bambinou1980's topic in PHP Coding Help
I would go a step further than ch0cu3r and use the product's row id as the array indexes name="product[$id]" name="price[$id]" then you know which product the data relates to when processing the form -
Submit $_POST with dynamically generated fields
Barand replied to bambinou1980's topic in PHP Coding Help
You are listing all the products. Each one has dropdown menu with only a single option. What is the point of that?