PHP - Wrapping While Loops In Divs
Hello guys!
I'm currently writing a bit of an image gallery code which is working pretty well however there seems to be a few issues with the sytling and layout of the gallery. (It's nothing flash just displays the images and caption in category order)
Now I am not sure how to go about doing this but is there any way to wrap each category in a div? I have the whole while loop wrapped in a div at present but I would like each category wrapped so I can style it a bit easier.
Can someone please point me in the right direction. Currently my code looks like this:
function Gallery(){ ini_set('display_errors',1); error_reporting(E_ALL); $conn = mysqli_connect("Why","Hello","There","Friends") or die('Cannot Connect to the database'); $q ="SELECT * FROM gallery_category AS c JOIN gallery_photos AS p ON p.categoryname = c.categoryname ORDER BY c.categoryid"; $result = mysqli_query($conn, $q) or trigger_error("Query Failed! SQL: $conn - Error: ".mysqli_error(), E_USER_ERROR); echo"<div class='gallery'>"; $categoryname=''; while($rows = mysqli_fetch_assoc($result)){ if ($categoryname != $rows['categoryname']) { $categoryname = $rows['categoryname']; echo"<div class='h'><h2>".$rows['categoryname']."</h2></div>"; } echo" <div class='gall'> <div class='gl-img'><img src='pathway/to/gallery/".$rows['photoname']."' alt='".$rows['categoryname']."'/></div> <div class='gl-cap'>".$rows['photocaption']."</div> </div>"; } echo"</div>"; $conn-> close(); }I don't know how to go about updating it so that it is this section that is wrapped whilst the "gall" class repeats (if that makes sense) echo"<div class='h'><h2>".$rows['categoryname']."</h2></div>"; } echo" <div class='gall'> <div class='gl-img'><img src='pathway/to/gallery/".$rows['photoname']."' alt='".$rows['categoryname']."'/></div> <div class='gl-cap'>".$rows['photocaption']."</div> </div>";So that it looks like this: <div class="category"> <div class="h"><h2>Category Title</h2></div> <div class="gall"> <div class="gl-img"><img src="pathway/to/gallery/polarbear2.jpg"></div> <div class="gl-cap">Polar Bear</div> </div> <div class="gall"> <div class="gl-img"><img src="pathway/to/gallery/polarbear2.jpg"></div> <div class="gl-cap">Polar Bear</div> </div> <div class="gall"> <div class="gl-img"><img src="pathway/to/polarbear2.jpg"></div> <div class="gl-cap">Polar Bear</div> </div> <div class="gall"> <div class="gl-img"><img src="pathway/to/gallery/polarbear2.jpg"></div> <div class="gl-cap">Polar Bear</div> </div> </div>Any and all help on how to achieve this would be appreciated!! thank you Edited by laurenetherington, 26 November 2014 - 12:10 PM. Similar TutorialsHi I'm creating a program that creates an image from an input using imagettftext(). Is there a way I can force a new line if the text exceeds the permitted image width? Thanks in advance Ste Hi guys! I have a specific requirement and need some help Say i have an external api library with Client class looking like this: class Client { some constants.. private props.., __construct() public static function create($url) { some code } public static function authenticate($token) { some code } ...dozen other methods } Current way of instantiating in every single other class where i need the client: use Client; MyFetchController { private $client; public function __construct() { $this->client = Client::create(getenv('URL'))->authenticate(getenv('TOKEN')) } $this->client->use other methods in Client class } Is there a way to wrap that class so i have external parameters loaded through .env file automaticaly available on new wrapper class intialization with something like this: MyFetchController { private $client; public function __construct(WrappedClient $client) { $this->client = $client } public function getData() { $data = $this->client->parse(...) } } Basically what i need is a class ready for DI in all other classes without inserting required auth and other parameters in constructor every single time I am trying to wrap a string with an anchor tag if it finds a match with a block of text. Here is an example text block: CONOCO 1'10x8 VC DF TP SGN||PRINCIPAL ILLUMINATION||ENG: CO3028TP_0VPR||DWG: CO200428||TO BE: DYED DIESEL (SPEC)|| The string I would want to wrap with a link would be "CO200428". The next problem is the drawings (what I'm searching the text for) has over 115,000 possibilities. The text blocks to search are over 1300. I have the drawing names stored in a simple mysql table...but doing a foreach takes forever...I imagine it will take even longer when looking in the text blocks... Is there a way to easily to do the anchor wrap? I don't know regex very well... How can I test if a chunk exceeds the image size and wrap that text to the next line. Not sure if I am even doing this correctly with my if statement. $text="Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem http://somelongurl.com/then-we-make-it-super-long-with-some-more/ Lorem Ipsum Lorem Ipsum Lorem Ipsum http://www.anotherlinks.com/"; $string_chunks = explode(' ', $text); foreach ($string_chunks as $chunk) { if($end_x + $chunk > $image_width){ $start_x = 5; $start_y += 20; } $coords = imagettfbbox($fontsize, $angle, $font, $chunk); $end_x = $coords[0] + $coords[4] + 10; $color_to_draw = is_a_url($chunk) ? $linkcolor : $black; imagettftext($im, $fontsize, $angle, $start_x, $start_y, $color_to_draw, $font, $chunk); $start_x += $end_x; } With this code I get: Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem http://somelongurl.com/then-we-make-it-super-long-with-some-more/ Lorem Ipsum Lorem Ipsum Lorem Ipsum http://www.anotherlinks.com/ What I would like to happen is something like: Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem http://somelongurl.com/then-we -make-it-super-long-with-some-more/ Lorem Ipsum Lorem Ipsum Lorem Ipsum http://www.anotherlinks.com/ This question has been asked almost exactly: Hi there, I know that php does not recognize onMouseOver events. however, Im wondering if their is a tricky way to get a javascript response from dynamic text. I am working on a nav menu in a wordpress site. wordpress has a function that displays a list of pages created in the admin. I want to be able to display a secondary list of pages when a top-level nav item is moused over. heres the code: <div id="top"> <script type="text/javascript"> function Show(who) { Clear(); divel=document.getElementById("drop"+who); divel.style.display="inline"; divel=document.getElementById("hn"+who); divel.className="selected"; } function Hide(who) { divel=document.getElementById("drop"+who); divel.style.display="none"; divel=document.getElementById("hn"+who); divel.className=""; } function Clear() { for (i=1;i<=3;i++) { divel=document.getElementById("drop"+i); divel.style.display="none"; divel=document.getElementById("hn"+i); divel.className=""; } } </script> <div id="home-nav" onMouseOut="Clear();"> <ul> <li id="hn1"><a href="wp_demo/profile" onMouseOver="Show('1')"><?php wp_list_pages(include=4&title_li=' ); ?></a></li> <li id="hn2"><a href="wp_demo/services" onMouseOver="Show('2')"><?php wp_list_pages( 'include=6&title_li=' ); ?></a></li> <li id="hn3"><a href="wp_demo/portfolio" onMouseOver="Show('3')"><?php wp_list_pages( 'include=8&title_li=' ); ?></a></li> <li id="hn4"><a href="wp_demo/contact" onMouseOver="Clear();"><?php wp_list_pages( 'include=12&title_li=' ); ?></a></li> <li id="hn5"><a href="wp_demo/hospitality" onMouseOver="Clear();"><?php wp_list_pages( 'include=10&title_li=' ); ?></a></li> </ul> </div> <!-- #homenav --> <div id="wrapper"> <div id="drop1" onMouseOver="Show('1')" onMouseOut="Hide('1')"> <img src="<?php bloginfo('stylesheet_directory'); ?>/images/blank.gif" alt="" width="960" height="8" border="0" /><br /> <div id="menu1"><ul> <?php wp_list_pages( 'child_of=4&sort_column=menu_order&title_li=' ); ?> </ul></div></div> <div id="drop2" onMouseOver="Show('2')" onMouseOut="Hide('2')"> <img src="<?php bloginfo('stylesheet_directory'); ?>/images/blank.gif" alt="" width="960" height="8" border="0" /><br /> <div id="menu2"><ul> <?php wp_list_pages( 'child_of=6&sort_column=menu_order&title_li=' ); ?> </ul></div></div> <div id="drop3" onMouseOver="Show('3')" onMouseOut="Hide('3')"> <img src="<?php bloginfo('stylesheet_directory'); ?>/images/blank.gif" alt="" width="960" height="8" border="0" /><br /> <div id="menu3"><ul> <?php wp_list_pages( 'child_of=8&sort_column=menu_order&title_li=' ); ?> </ul></div></div> Does anybody know a trick to get the dynamic text to be seen by the javascript? for example, if i put a inside the anchor tags before the php script starts, the onmouseover will register it, and display the secondary nav. I would place invisible divs with links on top of the dynamic text with z-index, but I want the text to be completely dynamic and the client to be able to add as many links as he/she wants. thanks! I am looking for a dynamic image script that handles word wrapping and possible font resizing. Basically, I have a dimensions, a font and a company name. So, lets say I am trying to make a 300x300 image for Bob's Boats in verdana. I want a script that not only creates the image, but tries to use up the available space (putting each word on a line, using a large font, horizontally and vertically aligning everything) Worst case I'll write my own, but the algorithm seems pretty complex. For example, lets say the companies name was: Chocolates in a Basket, it could be: Chocolates in a Basket Or: Chocolates in a Basket But never: Chocolates in a Basket Worst case, I might make it so that it supports 3 lines, but only ever puts the company name on 2 by default, and if the user wants to manually change it, they can. Even that isn't perfect though, because if the dimensions are something like 300x35, you wouldn't want a second line, let alone a 3rd. Hello everyone, I've already posted a question trying to resolve my problem, but I've changed my code so much, I figured it may be best just to post a new thread with a better explanation of what I'm trying to do. Anyway, here's what I've got: I have a database with 2 tables: products and reservations. I have 3 products for now, that can be reserved for any date. I'm trying to create a page that will display all of the available products for a specific date. The user selects the date they want to check, and then the code should check their selected date and compare it with the database to display everything that is available on that date. The database looks like this: Products Table: prodid prodname 01 item 1 02 item 2 03 item 3 Reservations Table: prodid resdate 01 02/22/2012 01 02/23/2012 03 02/22/2012 Here is the code that I have now: Code: [Select] <?php $resultres = mysql_query("SELECT `prodid` FROM reservations WHERE `resdate` = '$resdate'") or die (mysql_error()); while ($rowres = mysql_fetch_row($resultres)) { $resprodid = $rowres[0]; $resultavail = mysql_query("SELECT `prodid` FROM products WHERE `prodid` != '$resprodid'") or die (mysql_error()); while ($rowavail = mysql_fetch_row($resultavail)) { $prodid = $rowavail[0]; echo $prodid; echo $proname; } } ?> When I select 02/21/2012, I get no results, where I should get all three products as a result, because none of them are reserved on this date. When I select 02/22/2012, I get a result of these products, in this order: 02, 03, 01, 02. I should get a result of ONLY 02, because this is the only product that isn't reserved for this date. When I select 02/23/2012, I get a result of 02 and 03, which in this case would be the correct return, because 01 is the only one reserved for this date. Any idea what I'm doing wrong here and how to fix it? Any help is GREATLY appreciated! Hi All, and thanks for your help on my last problem now solved. What i am doing is building a job site and it needs info put in from job seekers regarding their qualifications. Some will have 2 some may have 10 and so on. I need to set up a system that I can put say 10 text boxes in a form that can then go in a MySQL. I only want the filled out boxes to go into the database and not the ones left blank. I have no idea as to where to start with but I think I need to loop through until it comes to the last filled out box but not enter the blank text boxes. i have no idea where to start on this one so if any one has thoughts on this it would be great. Thank you all for your help in the past. i have been given this task in in uni. i was wondering it some one would be so kind to help me with it. all i have so far is this <?php for($x=1; $x<=3; $x=$x+1) { print "$x <br>"; } if ($x== "A") { } ?> the task is below Generate a number between 1 & 3 If 1 then assign A, If 2 then assign B, If 3 then assign C Add to a wordstring (using string concatenation with .) We need 3 letters generated to give a 3 letter word For loop (3) Generate a number between 1 & 3 If 1 then assign A, If 2 then assign B, If 3 then assign C Add to a wordstring (string concatenation with .) End loop Print 3 letter word i am a student and trying to figure out why this is not working for me i am trying to pull from my .txt file and add the lines. $numOrders should be 20 and $numCopies should be 52. it keeps telling me --Warning: feof() expects parameter 1 to be resource, string given on line 25. i really am having big trouble trying to understand how to use the while loop for this. any help would be great. here is my html code <html> <head> <title>Software1</title> <link rel ="stylesheet" type="text/css" href="sample.css" /> </head> <body> <h1>SOFTWARE ORDERS: REPORT</h1> <form action = "software1Boles.php" method = "post" > <p><input type = "submit" value = "Display the Report" /></p> </form> </body> </html> my php code <?php $totalCopies = $_POST['totalCopies']; $totalOrders = $_POST['totalOrders']; $nextOrder = $_POST['nextOrder']; $totalCopies = 0; $totalOrders = 0; $orderFile = fopen("ordersBoles.txt", "r"); $nextOrder = fgets ($orderFile); while (!feof($nextOrder)) { list($totalCopies, $totalOrders) = explode (":", $nextOrder); if ($totalCopies >=1 and $totalCopies >=1) { $totalCopies = $totalCopies + $nextOrder; $totalOrder = $totalOrder + $nextOrder; } $nextOrder = fgets($orderFile);} fclose($orderFile); print ("<h1>SOFTWARE ORDERS: REPORT</h1>"); print ("<p>TOTAL COPIES ORDERED: $totalCopies</p>"); print ("<p>TOTAL ORDERS: $totalOrders</p>"); ?> </body> </html> and my .txt file 1 Linux:1 2 Macintosh:1 3 Windows:1 4 Macintosh:1 5 Macintosh:2 6 Linux:5 7 Macintosh:10 8 Windows:10 9 Macintosh:1 10 Windows:1 11 Windows:1 12 Linux:1 13 Macintosh:5 14 Linux:4 15 Windows:1 16 Macintosh:1 17 Windows:1 18 Linux:2 19 Macintosh:2 20 Windows:1 21 anything you can give me would help alot, thanks I do not have the option that I know of to do a LIMIT 10 in my odbc query. How can I get it to count the number of while loops and stop at 10? Code: [Select] while (odbc_fetch_row($rs)) { } Did PHP a few years ago and for the life of me i can't work out why this doesn't work: Code: [Select] for ( $counter2=1; $counter2 < 1000; $counter2++) { for ( $counter1=1; $counter1 < 1000; $counter1++) { echo "Fixed bug"; } } and yet this does Code: [Select] for ( $counter2=1; $counter2 < 100; $counter2++) { for ( $counter1=1; $counter1 < 100; $counter1++) { echo "Fixed bug"; } } I tested it because I have a rather large amount of data to retrieve from an XML file and i kept receiving errors Hi guys
I just wanted to make a short post and talk about an issue im having and maybe get some advice. I know that loops aren't that hard to grasp and I shouldn't be having this much trouble understanding them but im just not getting it. I dont really know how to explain it but loops (like looping through mysql in an update statement) is foren to me and i was wondering if anyone had any advice for maybe learning them or maybe something im just over looking or maybe some good tutorials. I really want to get them down and start using them in scripts i right but im getting so frustrated because i constantly hear how easy they are and im just not getting it. if i wasn't clear please let me know and ill try to explain more clearly thanks guys JL i am having issues with my class work. this needs to start at the $startNum and end with the $endNum and display results in the increments submitted by the html. I can not get it to print out correctly it is supposed to say: The square of $startNum is $square //until it hits the end number and is going up by the correct increments, It wont go up by any increments and only shows a list if i leave out the increment-----any suggestions??? please i have to keep it very simple since i am just learning-ty html </head> <body> <h1>Squares</h1> <p> <form action = "squares2Boles.php" method = "post" > <p>Start with: <input type = "text" size = "5" name = "startNum" /> </p><p>End with: <input type = "text" size = "5" name = "endNum" /> </p><p>Increment by: <input type = "text" size = "5" name = "increment" /> </p><p><input type = "submit" value = "Display the Squares" /></p> </form> </body> </html> my php: <html> <head> <title>Squares</title> <link rel ="stylesheet" type="text/css" href="sample.css" /> </head> <body> <?php $startNum = $_POST['startNum']; $endNum = $_POST['endNum']; $increment = $_POST['increment']; $square = $_POST['square']; print ("<h1>SQUARES</h1><hr />"); for ($startNum = $startNum; $startNum <= $endNum; $startNum = $startNum + $increment) { $square = $startNum * $startNum; print ("The square of $startNum is $square<br />"); } print ("<hr />"); ?> </body> </html> I created the code below and it outputs the following number output: 1111111773526604115551144611337112281111221111345 6789101121111100352004110051100611007110081100221 1003456789101141111100352004110051100611007110081 1002211003456789101151111100352004110051100611007 1100811002211003456789101123111110035200411005110 06110071100811002211003456789101134 Anyways if you look at the code, I need to be able to add the values on a single colony, idcol, with the same type and subtype, btid/stid, generated as the $prod variable. buildings with the same subtype generate the same resource, in this case, several iron mines and a single copper mine are present, but there are also 9 other types of resource buildings. How do I collect on the prods which go together? Code: [Select] <?php $dbhost = 'localhost:3306'; $dbuser = 'lordofth_aos'; $dbpass = 'flarge'; $dbname = 'lordofth_aos'; $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); $query="SELECT id FROM users"; $result=mysql_query($query); $num=mysql_num_rows($result); $i=0; $idn=1; while($i<$num){ $queryii="SELECT idcol FROM colony WHERE id=$idn"; $resultii=mysql_query($queryii); $numt=mysql_num_rows($resultii); $id=mysql_result($result, $i); echo $id; $ii=0; $ibt=1; while($ii<$numt){ $querypty="SELECT btid FROM ptypes WHERE btid=$ibt"; $resultpty=mysql_query($querypty); $numpty=mysql_num_rows($resultpty); $idcol=mysql_result($resultii, $ii); echo $idcol; $bt=0; $btid=mysql_result($resultpty, $bt); echo $btid; $bte=1; while($bt<$numpty){ $querystid="SELECT stid FROM ptypes WHERE stid=$bte"; $resultstid=mysql_query($querystid); $numstid=mysql_num_rows($resultstid); $st=0; $stid=mysql_result($resultstid, $st); echo $stid; $ste=1; while($st<$numstid){ $bidum=0; $querybpid="SELECT bpid FROM blueprints WHERE btid=$btid AND stid=$stid"; $resultbpid=mysql_query($querybpid); $numbpid=mysql_num_rows($resultbpid); while($bidum<$numbpid){ $bidumb=0; $bidumbg=mysql_result($resultbpid, $bidum, 'bpid'); echo $bidumbg; $querybid="SELECT COUNT(bid) AS count, workers, efficiency FROM buildings INNER JOIN blueprints ON buildings.bpid=blueprints.bpid WHERE blueprints.btid=$btid AND blueprints.stid=$stid AND buildings.bpid=$bidumbg AND buildings.idcol=$idcol"; $resultbid=mysql_query($querybid); $staff=mysql_result($resultbid, $bidumb, 'workers'); $effic=mysql_result($resultbid, $bidumb, 'efficiency'); $count=mysql_result($resultbid, $bidumb, 'count'); echo $staff; echo $effic; echo $count; $prod=$staff*$effic*$count; echo $prod; $bidumb++; $bidum++; } $st++; $ste++; } $bt++; $bte++; } $ii++; } $i++; $idn++; } ?> I will start off by saying , I am a NooB. i understand concepts , but have trouble with figuring out the proper way to code thing out. I am trying to use an HTML form that the user can input two numbers, and use those numbers to create a table to be echoed back. echo "<form action='action.php' method='post' name='columns'> Enter number of columns and rows to be displayed:<br /> Columns:<input type='text' name='colnums' size=3 maxlength=6 />Rows :<input type='text' name='rownums' size=3 maxlength=6 /> <br /><input type='submit' value='submit' /></form>" ; This is my form. I have made a loop that will display but not in the fashion i would like it to. for ($createColumns=$_POST['colnums']; $createColumns<=10 ; $createColumns++) { echo "<td>" . $createColumns . "</td>" ; } I am looking to write a script that only needs the user input for the number of rows and columns to be displayed. I think I need a range function or possibly an array for the possible numbers that can be input, but I'm not sure. The loop I've made has a top limit and an increment that i don't think i need. If you have any ideas I would greatly appreciate it. Man this is really driving me nuts. I can see my array values with echo inside the loop but outside nothing is shown. What am I missing here? If anything it should show the last value in the loop. while ($x = mysql_fetch_array($query_name,MYSQL_NUM)) { echo $x[0]; } echo $x[0]; My goal here is to have it foreach group from the DB it'll create a new frameset with the groupName as the legend and then under each of those it will create another foreach for each of the fields inside of that group that has a enabled value of 0 from the db. Both queries work correctly. I just get trouble writing foreach loops and I'm not sure how this one is going to work out. Code: [Select] <?php session_start(); // Access the existing session // Include the database page require ('../../inc/dbconfig.php'); $defaultCharID = $_SESSION['defaultCharID']; $styleIDQuery = " SELECT characters.styleID FROM characters WHERE characters.ID = '" . $defaultCharID . "'"; $styleIDResult = mysqli_query ( $dbc, $styleIDQuery ); // Run The Query $row = mysqli_fetch_array( $styleIDResult, MYSQL_ASSOC ); $styleID = $row[ 'styleID' ]; $biofieldsQuery = " SELECT fields.*, groups.* FROM fields INNER JOIN groups ON fields.groupID = groups.ID WHERE groups.styleID = '" . $styleID . "' AND fields.styleID = '" . $styleID . "' AND groups.enabled = 0 AND fields.enabled = 0"; $biofieldsResult = mysqli_query ( $dbc, $biofieldsQuery ); // Run The Query $row = mysqli_fetch_array( $biofieldsResult, MYSQL_ASSOC ); $groupName = $row[ 'groupName' ]; ?> <script type="text/javascript"> $(document).ready(function() { }); </script> <!-- Title --> <div id="title" class="b2"> <h2>Character Management</h2> <!-- TitleActions --> <div id="titleActions"> <!-- ListSearch --> <div class="listSearch actionBlock"> <div class="search"> <label for="search">Recherche</label> <input type="text" name="search" id="search" class="text" /> </div> <div class="submit"> <button type="submit" id="search-button" class="button"><strong><img src="img/icons/search_48.png" alt="comments" class="icon "/></strong></button> </div> </div> <!-- /ListSearch --> </div> <!-- /TitleActions --> </div> <!-- Title --> <!-- Inner Content --> <div id="innerContent"> <form action="#" id="bioForm" > <fieldset> <legend><?php echo $groupName; ?></legend> <div class="field required"> <label for="matchType">Match Type Name</label> <input type="text" class="text" name="matchType" id="matchType" title="Match Type Name"/> <span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span> </div> <input type="submit" class="submit" name="submitMatchType" id="submitMatchType" title="Submit Match Type" value="Submit Match Type"/> </fieldset> </form> </div> <!-- /Inner Content --> Hello, I am new in php and have made only little so fare. I am trying to make a script, but the script stops. I dont know how loops work. Quote <?php $lenkzeit = 4.5; $lenkunter = 1; $ruhezeit1 = 11; $ruhezeit2 = 9; $stunden1 = 0; $km = 1000; $kmh = 80; $stunden0 = $km/$kmh; $stunden01 = $stunden0; while ($stunden01 > $lenkzeit){ $stunden1 = $stunden1 + $lenkzeit + 1; $stunden01 = $stunden01 - $lenkzeit; //Stops here if ($stunden01 > $lenkzeit) { $stunden1 = $stunden1 + $lenkzeit + $ruhezeit; $stunden01 = $stunden01 - $lenkzeit; } } if ($stunden01 < $lenkzeit) { $srunden1 = $stunden1 + $stunden01; $stunden01 = 0; } echo $srunden1; ?> |