SicKn3sS Posted September 15, 2007 Share Posted September 15, 2007 Hello, Is there a way to echo 30 fields without telling the php app each field name? my code looks like this: $category=TEM; $query="SELECT * FROM `Hitachi Consumables` WHERE Instrument='$instrument'"; $result=mysql_query($query,$sql); $num=mysql_numrows($result); mysql_close(); echo "$instrument <br><br>"; $i=0; while ($i < $num) { $filament=mysql_result($result,$i,"Filament"); $filCart=mysql_result($result,$i,"Filament Cartridge"); $l6=mysql_result($result,$i,"LAB-6 or Consumable Emitter"); $updatedBy=mysql_result($result,$i,"Updated by"); $fixedCondenserAperture=mysql_result($result,$i,"Fixed Condenser Aperture"); $movableAperture=mysql_result($result,$i,"EDX Objective Movable Aperture"); $conMovableAperature=mysql_result($result,$i,"Condenser Movable Aperture"); $objMovableAperature=mysql_result($result,$i,"Objective Movable Aperture"); $beamMA=mysql_result($result,$i,"Beam Monitor Aperture"); $DBAOBJ=mysql_result($result,$i,"DPA or OBJ Fixed Aperture"); $SEMA=mysql_result($result,$i,"Selected Area Movable Aperture"); $viewScreen=mysql_result($result,$i,"Viewing Screen"); $sViewScreen=mysql_result($result,$i,"Spot Viewing Screen"); $PMT=mysql_result($result,$i,"PMT"); $STEMs=mysql_result($result,$i,"STEM Scintillator"); $BSEDetector=mysql_result($result,$i,"BSE Detector"); $trackBall=mysql_result($result,$i,"Track Ball"); $OVS=mysql_result($result,$i,"Outer Viewing Screen"); $wehneltAssembly=mysql_result($result,$i,"Wehnelt Assembly"); $wehneltCS=mysql_result($result,$i,"Whenelt Contact Spring"); $wehneltfC=mysql_result($result,$i,"Wehnelt for Cartridge"); echo "<b>Filament:</b> $filament<br> <b>Filament Cartridge:</b> $filCart<br> <b>LAB-6 or Consumable Emitter:</b> $l6<br> <b>Fixed Condenser Aperture:</b> $fixedCondenserAperture<br> <b>EDX Objective Movable Aperture:</b> $movableAperture<br> <b>Condenser Movable Aperture:</b> $conMovableAperature<br> <b>Objective Movable Aperture:</b> $objMovableAperature<br> <b>Beam Monitor Aperture:</b> $beamMA<br> <b>DPA or OBJ Fixed Aperture:</b> $DBAOBJ<br> <b>Selected Area Movable Aperture:</b> $SEMA<br> <b>Viewing Screen:</b> $viewScreen<br> <b>Spot Viewing Screen:</b> $sViewScreen<br> <b>PMT:</b> $PMT<br> <b>STEM Scintillator:</b> $STEMs<br> <b>BSE Detector:</b> $BSEDetector<br> <b>Track Ball:</b> $trackBall<br> <b>Outer Viewing Screen:</b> $OVS<br> <b>Wehnelt Assembly:</b> $wehneltAssembly<br> <b>Whenelt Contact Spring:</b> $wehneltCS<br> <b>Wehnelt for Cartridge:</b> $wehneltfC<br>"; $i++; } I would like to somehow shrink all the code down after "while ($i < $num) {". Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 15, 2007 Share Posted September 15, 2007 make an array of all of those field names, and then do a foreach Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 15, 2007 Share Posted September 15, 2007 Use mysql_fetch_assoc in stead of mysql_result eg: $category = TEM; $query = "SELECT * FROM `Hitachi Consumables` WHERE Instrument='$instrument'"; $result = mysql_query($query,$sql); mysql_close(); echo "<p>$instrument</p>\n"; while($row = mysql_fetch_assoc($result)) { foreach($row as $field => $value) { echo '<b>' . $field . ':</b> ' . $value . "<br />\n"; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.