perezf Posted March 20, 2008 Share Posted March 20, 2008 I created a script that works perfectly fine, the only thing is i dont know how to integrate a while loop or a for loop so i can call the array elements from a database. can anyone help me please? This is the script that works <ul> <?php $seolinks = array( '<li><a href="breast_augmentation.html">Breast Augmentation</a></li>', '<li><a href="breast_augmentation_artistry.html">Artistry in Breast Augmentation</a></li>', '<li><a href="breast_augmentation_forming_beautiful_breast.html">Breast Augmentation: Forming a Beautiful Breast</a></li>', '<li><a href="breast_augmentation_after_the_operation.html">Breast Augmentation: After the Operation</a></li>', '<li><a href="breast_augmentation_facts.html">Facts about Breast Augmentation</a></li>', '<li><a href="breast_augmentation_modern_era.html">Modern Era of Breast Augmentation</a></li>', '<li><a href="breast_augmentation_and_enlargement.html">Breast Augmentation / Breast Enlargement</a></li>', '<li><a href="breast_augmentation_the_surgery.html">The Breast Augmentation Surgery</a></li>', '<li><a href="breast_augmentation_recovering_from_surgery.html">Recovering from Breast Augmentation Surgery</a></li>', '<li><a href="breast_augmentation_are_in.html">Breast Augmentations are In</a></li>', '<li><a href="breast_augmentation_atmosphere.html">Where to do the Breast Augmenation Surgery</a></li>', '<li><a href="breast_augmentation_the_surgeon.html">Choosing a Surgeon for Breast Augmentation</a></li>', '<li><a href="breast_implants.html">Breast Implants</a></li>', '<li><a href="breast_implants_procedure.html">Breast Implants: Breast Implant Procedure</a></li>', '<li><a href="breast_implants_silicone_vs_saline_implants.html">Breast Implants: Silicone vs Saline Implants</a></li>', '<li><a href="breast_implants_the_shape.html">Breast Implants: The Shape</a></li>', '<li><a href="breast_implants_smooth_vs_textured_surface.html">Breast Implants: Smooth vs Textured Surface</a></li>', '<li><a href="breast_implants_behind_or_in_front_of_the_muscle.html">Breast Implants: Behind or In Front of the Muscle</a></li>', '<li><a href="breast_implants_incision_placement.html">Breast Implants: Incision Placement</a></li>', '<li><a href="breast_implants_implant_manufacturers.html">Breast Implants: Implant Manufacturers</a></li>', '<li><a href="breast_implants_implant_size.html">Breast Implants: Implant Size</a></li>', ); $numbertodisplay = "8"; $random_disp = array_rand($seolinks, $numbertodisplay); for ($num=0; $num <=$numbertodisplay; $num++) { print $seolinks[$random_disp[$num]] . "\n"; } ?> </ul> this is the code that i want to loop in the array instead of writing them all out like above. print '<li><a href="http://retailsneakerstore.com/brand-name-' . $catname . '---' . $catid . '" title="'.$catname.'">'.$catname.'</a></li>' . "\n" Link to comment https://forums.phpfreaks.com/topic/97031-i-need-help-with-a-script-on-arrays-and-loops/ Share on other sites More sharing options...
Jeremysr Posted March 20, 2008 Share Posted March 20, 2008 So something like this? $result = mysql_query("SELECT catname, catid FROM table"); while ($values = mysql_fetch_array($result)) { print '<li><a href="http://retailsneakerstore.com/brand-name-' . $values['catname'] . '---' . $values['catid'] . '" title="'.$values['catname'].'">'.$values['catname'].'</a></li>' . "\n"; } Oh, if you want to randomly show only 8, then use this query: $result = mysql_query("SELECT catname, catid FROM table ORDER BY RAND() LIMIT 8"); Link to comment https://forums.phpfreaks.com/topic/97031-i-need-help-with-a-script-on-arrays-and-loops/#findComment-496553 Share on other sites More sharing options...
Laxidasical Posted March 20, 2008 Share Posted March 20, 2008 Jeremysr's example should fit the bill! I run into situations sometimes where I need to hold all of the rows in a multi-demensional array. In that case, this will work... $result_id = mysql_query("SELECT catname, catid FROM table"); if ($result_id != FALSE && mysql_num_rows($result_id) > 0) { $i = 0; while($results = mysql_fetch_assoc($result_id )) { // LOOP THROUGH EACH ROW AND ASSIGN VALUES TO A MULTI-DEMENSIONAL ARRAY foreach($results as $key => $value) { $rows[$i][$key] = $value; { // INCREASE $i FOR NEXT ROW $i++; } } Now you have a single variable that has all of your query results. To display... foreach($rows as $row) { echo '<li><a href="http://retailsneakerstore.com/brand-name-' . $row['catname'] . '---' . $row['catid'] . '" title="' . $row['catname'] . '">' . $row['catname'] . '</a></li>' . "\r\n"; } I find myself using "foreach" loops with arrays far more than "while" or "for". I hope that helps! Link to comment https://forums.phpfreaks.com/topic/97031-i-need-help-with-a-script-on-arrays-and-loops/#findComment-496609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.