[SOLVED] While / For Loop - Extract data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

[SOLVED] While / For Loop - Extract data

Post 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",";

  }
Last edited by facets on Sat Sep 24, 2005 5:14 am, edited 1 time in total.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

Code: Select all

while ($row=mysql_fetch_object($query)){
echo $row->field1;
echo $row->field2;
}
you get hte idea:)
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

how about

Code: Select all

echo implode(',', $csvOutput);
instead
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

you could mysql_fetch_array, just a personal preference. his idea works too :-D I'm sleepy:-D
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

say i use echo implode(',', $csvOutput);
how would I extract all rows?
it seems to only grab the 1st row.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

look up fgetcsv on php.net . There's a good example of looping a csv file
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

I think I actually need to dot he reverse of fgetcsv.
Extract all rows from DB and export to CSV.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post 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 .
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

take out your $query_data line. And then use mysql_fetch_assoc($query)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

got it sorted.
thanks all for you assistance!
Post Reply