Page 1 of 1

[SOLVED] While / For Loop - Extract data

Posted: Sat Sep 24, 2005 12:09 am
by facets
Hey Gang,

Having some troubles with this loop.
Any ideas on how I could ensure every row from the DB was outputted?
Do I need a while loop instead?

Code: Select all

//SQL Extract
$query = mysql_query("SELECT *, aupapercategory.paperCategory FROM ausapapersummary
LEFT JOIN aupapercategory ON ausapapersummary.paperCategoryId = aupapercategory.papercategoryId
LEFT JOIN austock ON ausapapersummary.stockId = austock.StockId
LEFT JOIN auadhesive ON ausapapersummary.adhesiveId = auadhesive.adhesiveId
LEFT JOIN auliner ON ausapapersummary.linerId = auliner.linerId
LEFT JOIN ausupplier ON ausapapersummary.supplierId = ausupplier.supplierId
LEFT JOIN ausuitability ON ausapapersummary.suitabilityFoil = ausuitability.suitabilityId
LEFT JOIN admins ON ausapapersummary.ausapaperUserId = admins.id WHERE 1=1");

// For loop
$csvOutput = array("$colloPaperName", "$paperCategory", "$manufacturerName", "$supplier", "$availability", "$features", "$limitations", "$productExamples", "$suitabilityFoil", "$suitabilityYellowLight", "$suitabilityLabel", "$suitabilityOpacity", "$suitabilityBronze", "$suitabilityScreen", "$suitabilityIceBucket", "$stockDescription", "$basisWeight", "$caliper", "$wetTensileStrenghtCD", "$wetTensileStrenghtMD", "$dryTensileStrengthCD", "$dryTensileStrengthMD", "$opacity", "$gloss", "$moistureContent", "$brightness", "$relativeHumitity", "$absorbtion", "$smoothness", "$tearStrengthCD", "$tearStrengthMD", "$burstStrength", "$pH", "$adhesiveDescription", "$thickness", "$initialTack", "$ballTack", "$peelAdhesion180", "$peelAdhesion90", "$shear", "$shelfLife", "$minApplicationTemp", "$serviceMin", "$serviceMax", "$linerDescription", "$grammage", "$caliper", "$strengthCD", "$strengthMD", "$translucency", "$shear", "$humidity", "$smoothnessWS", "$smoothnessFS", "$releaseForceLow", "$releaseForceHigh");

for($x = 0; $x<count($csvOutput); $x++) {

  echo $csvOutput[$x];echo",";

  }

Posted: Sat Sep 24, 2005 12:15 am
by Charles256

Code: Select all

while ($row=mysql_fetch_object($query)){
echo $row->field1;
echo $row->field2;
}
you get hte idea:)

Posted: Sat Sep 24, 2005 12:16 am
by sweatje
how about

Code: Select all

echo implode(',', $csvOutput);
instead

Posted: Sat Sep 24, 2005 12:16 am
by Charles256
you could mysql_fetch_array, just a personal preference. his idea works too :-D I'm sleepy:-D

Posted: Sat Sep 24, 2005 12:49 am
by facets
say i use echo implode(',', $csvOutput);
how would I extract all rows?
it seems to only grab the 1st row.

Posted: Sat Sep 24, 2005 12:55 am
by s.dot
look up fgetcsv on php.net . There's a good example of looping a csv file

Posted: Sat Sep 24, 2005 1:00 am
by facets
I think I actually need to dot he reverse of fgetcsv.
Extract all rows from DB and export to CSV.

Posted: Sat Sep 24, 2005 1:03 am
by s.dot
oooh, i'm sorry.. i didn't read properly.

Code: Select all

while($array = mysql_fetch_assoc($result))
{
   echo implode(',' $array).'<BR />';
}
That should give you each row of your database per line

Posted: Sat Sep 24, 2005 2:42 am
by facets
thanks for the help everyone.
i/m getting an error :
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in c:\program files\easyphp1-8\www\collotype\materialsregister\csv.php on line 118

Code: Select all

$query = mysql_query("SELECT *, aupapercategory.paperCategory FROM ausapapersummary
LEFT JOIN aupapercategory ON ausapapersummary.paperCategoryId = aupapercategory.papercategoryId
LEFT JOIN austock ON ausapapersummary.stockId = austock.StockId
LEFT JOIN auadhesive ON ausapapersummary.adhesiveId = auadhesive.adhesiveId
LEFT JOIN auliner ON ausapapersummary.linerId = auliner.linerId
LEFT JOIN ausupplier ON ausapapersummary.supplierId = ausupplier.supplierId
LEFT JOIN ausuitability ON ausapapersummary.suitabilityFoil = ausuitability.suitabilityId
LEFT JOIN admins ON ausapapersummary.ausapaperUserId = admins.id");

$query_data = mysql_fetch_array($query); 
      
$summaryId = $query_data['summaryId'];
$paperCategoryId = $query_data['paperCategoryId'];
$colloPaperName = $query_data['colloPaperName'];
$manufacturerName = $query_data['manufacturerName'];
$cpl = $query_data['cpl'];
$stockId = $query_data['stockId'];
$adhesiveId = $query_data['adhesiveId']; (etc)

$csvOutput = array("$colloPaperName", "$paperCategory", "$manufacturerName", "$supplier", "$availability""); (etc)
	
while($csvOutput = mysql_fetch_assoc($query_data))
{
   echo implode(',', $csvOutput).'<BR />';
}
I'm sure i've balls'd it up somewhere..
also (etc) i've removed the other variables .

Posted: Sat Sep 24, 2005 4:57 am
by s.dot
take out your $query_data line. And then use mysql_fetch_assoc($query)

Posted: Sat Sep 24, 2005 5:14 am
by facets
got it sorted.
thanks all for you assistance!