Jump to content

excel export


searls03

Recommended Posts

ok, so I am not good with arrays, they confuse me.  how can I set this up so that $data is all results that are returned from the database that correspond to it? 

 

$data = array( 
array("firstname" => '$firstname', "lastname" => '$lastname', "age" => '$age'));

 

the original code looked like:

<?PHP $data = array( array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25), array("firstname" => "Amanda", "lastname" => "Miller", "age" => 18), array("firstname" => "James", "lastname" => "Brown", "age" => 31), array("firstname" => "Patricia", "lastname" => "Williams", "age" => 7), array("firstname" => "Michael", "lastname" => "Davis", "age" => 43), array("firstname" => "Sarah", "lastname" => "Miller", "age" => 24), array("firstname" => "Patrick", "lastname" => "Miller", "age" => 27) ); ?>

 

but that won't do it for me.  I need it so that the results from the database will automatically be filled in.  basically, everyone that is in the database, so when a new member is added, they will show up too.  please help

Link to comment
Share on other sites

this gives me no results.  I echo everything, no results, just a bunch of errors :(

 

$data = array();

$query = mysql_query("SELECT * FROM members where userid !='$userid'");

while($resouce = mysql_fetch_assoc($query))
   $data[] = $resource;
  
header("Content-Type: text/plain"); $flag = false; foreach($data as $row) { if(!$flag) { 
# display field/column names as first row 
echo implode("\t", array_keys($row)) . "\r\n"; $flag = true; } echo implode("\t", array_values($row)) . "\r\n"; } 

 

<br />

<b>Warning</b>:  array_keys() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<br />

<b>Warning</b>:  array_values() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<br />

<b>Warning</b>:  array_values() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<br />

<b>Warning</b>:  array_values() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<br />

<b>Warning</b>:  array_values() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<br />

<b>Warning</b>:  array_values() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<br />

<b>Warning</b>:  array_values() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<br />

<b>Warning</b>:  array_values() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<br />

<b>Warning</b>:  array_values() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<br />

<b>Warning</b>:  array_values() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<br />

<b>Warning</b>:  array_values() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<br />

<b>Warning</b>:  array_values() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<br />

<b>Warning</b>:  array_values() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<br />

<b>Warning</b>:  array_values() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<br />

<b>Warning</b>:  array_values() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<br />

<b>Warning</b>:  array_values() expects parameter 1 to be array, null given in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/searls04/public_html/secure/print.php</b> on line <b>45</b><br />

 

<?php
session_start(); // Must start session first thing
/* 
Created By Adam Khoury @ www.flashbuilding.com 
-----------------------June 20, 2008----------------------- 
*/
// Here we run a login check
if (!isset($_SESSION['id'])) { 
   echo 'Please <a href="login.php">log in</a> to access your account';
   exit(); 
}
//Connect to the database through our include 
include_once "connect_to_mysql.php";
// Place Session variable 'id' into local variable
$userid = $_SESSION['id'];
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM members");
while($row = mysql_fetch_array($sql)){
$name = $row['name'];
$phone = $row["phone"];
$username = $row["username"];
$address = $row["address"];
$city = $row["city"];
$state = $row["state"];
$zip = $row["zip"];
$cell = $row["cell"];
$email = $row["email"];
$accounttype = $row["accounttype"];
$rank = $row["rank"];
$badges = $row["badges"];
}

    

    
$data = array();

$query = mysql_query("SELECT * FROM members where userid !='$userid'");

while($resouce = mysql_fetch_assoc($query))
   $data[] = $resource;
  
header("Content-Type: text/plain"); $flag = false; foreach($data as $row) { if(!$flag) { 
# display field/column names as first row 
echo implode("\t", array_keys($row)) . "\r\n"; $flag = true; } echo implode("\t", array_values($row)) . "\r\n"; } 





?>

Link to comment
Share on other sites

ok, I figured out a different way.  all I need to know how to do is make it so that the results from a specific mysql query are shown.  I made it so that all results from the table are shown, but i need to know how to make it so that print.php will export all results from a specific query from this code:

<?php
if (isset($_POST['submitted'])) {
include('connect1.php');
$category = $_POST['category'];
$criteria = $_POST['criteria'] ;
$query = ("SELECT name, badges, rank, userid FROM members WHERE $category LIKE '%".$criteria."%'");
$result = mysqli_query($dbcon, $query) or die('error getting data');
$num_rows = mysqli_num_rows($result);
echo "$num_rows results found";
echo "<table width=\"896\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
echo "<tr bgcolor=\"#F7E496\"><td bgcolor=\"#F7E496\"><strong>name</strong></td><td bgcolor=\"#F7E496\" ><strong>Merit Badges</strong></td><td  bgcolor=\"#F7E496\"><strong>Rank</strong></td><td bgclor=\"#F7E496\"></td></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {$color = ($color == 'white')?'#fffccc':'white';
echo "<tr bgcolor='$color'><td>
";
echo $row['name'];
echo "
</td><td>
<form action=\"scout.php\" method=\"post\">
<input type=\"hidden\" name=\"userid[]\" id=\"userid\" value='".$row['userid']."'>
  
    <textarea name=\"badges[]\" id=\"badges\" cols=\"40\" rows=\"3\" type=\"textarea\">".$row['badges']."</textarea></td><td>
    <span class=\"adfa\"> 
    </span>
   
  
  <select name=\"rank[]\" id=\"rank\">
    <option value=\"Scout\">Scout</option>
    <option value=\"Tenderfoot\">Tenderfoot</option>
    <option value=\"Second Class Scout\">Second Class Scout</option>
    <option value=\"First Class Scout\">First Class Scout</option>
    <option value=\"Star Scout\">Star Scout</option>
    <option value=\"Life Scout\">Life Scout</option>
    <option value=\"Eagle Scout\">Eagle Scout</option>
    <option value='".$row['rank']."' selected=\"selected\">".$row['rank']."</option>

</td><td>";



}
echo "</td></tr>";

echo "</table>";

echo "<input type=\"submit\" name='submit' id=\"submit\" value=\"Save\" />

</form>";

}
?>

 

<?PHP 
include_once "connect_to_mysql.php";
include ('scoutmanager');
// Original PHP code by Chirp Internet: www.chirp.com.au // Please acknowledge use of this code by including this header.
function cleanData(&$str) { $str = preg_replace("/\t/", "\\t", $str); $str = preg_replace("/\r?\n/", "\\n", $str); if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; } // file name for download 
$filename = "website_data_" . date('Ymd') . ".xls"; header("Content-Disposition: attachment; filename=\"$filename\""); header("Content-Type: application/vnd.ms-excel"); $flag = false; $result = mysql_query("SELECT name, badges, rank FROM members") or die('Query failed!'); while(false !== ($row = mysql_fetch_assoc($result))) { if(!$flag) { // display field/column names as first row 
echo implode("\t", array_keys($row)) . "\r\n"; $flag = true; } array_walk($row, 'cleanData'); echo implode("\t", array_values($row)) . "\r\n"; } ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.