PHP - Displaying Results From Mysql Database In Columns
Hi,
This has been baffling me for a couple hours now and i cant seem to figure it out. I have some code which creates an array and gets info from a mysql database and then displays in a list. This works great but after adding more and more rows to my database the list is now becoming quite large and doesnt look great on my site. Is it possible to split the list into multiple columns of about 25 and if possible once 3 or 4 columns have been created start another column underneath. To help explain i would be looking at a layout as follows: Code: [Select] line 1 line 1 line 1 line 2 line 2 line 2 ... ... ... line 25 line 25 line 25 line 1 line 1 line 1 line 2 line 2 line 2 ... ... ... line 25 line 25 line 25Im guessing there should be some sort of if statement to check how many items are being displayed and to create a new column if necessary. Is this correct? Thanks, Alex Similar TutorialsHi I have got results being displayed after clicking the search button in a form on my home page but it brings up all the results which is ok but how do I get onlt the results a user searches for for example a location or property type etc as its for a property website The coding is below for the results page Also sorry how do I add a background image to the php page, I tried using css but wouldn't work Code: [Select] <style type="text/css"> body {background-image:url('images/greybgone.png');} </style> <?php mysql_connect ("2up2downhomes.com.mysql", "2up2downhomes_c","mD8GsJKQ") or die (mysql_error()); mysql_select_db ("2up2downhomes_c"); echo $_POST['term']; $sql = mysql_query("select * from properties where typeProperty like '%$term%' or location like '%$term%'"); while ($row = mysql_fetch_array($sql)){ echo 'Type of Property: '.$row['typeProperty']; echo '<br/> Number of Bedrooms: '.$row['bedrooms']; echo '<br/> Number of Bathrooms: '.$row['bathrooms']; echo '<br/> Garden: '.$row['garden']; echo '<br/> Description: '.$row['description']; echo '<br/> Price: '.$row['price']; echo '<br/> Location: '.$row['location']; echo '<br/> Image: '.$row['image']; echo '<br/><br/>'; } ?> Code: [Select] <?php $result = mysql_query("SELECT * FROM FamilyTbl INNER JOIN PeopleTbl ON (FamilyTbl.Name_ID = PeopleTbl.NameID) WHERE FamilyTbl.House_ID = '$address' ORDER BY NameLast, NameFirst ") OR die(mysql_error()); WHILE ($row = mysql_fetch_array($result) ) { echo $row[NameLast]. ", ". $row[NamePrefix]. " ". $row[NameFirst]. " ". $row[NameMiddle]. $row[NameSuffix]. " "; } ?> OK, some of these queries return A LOT of names. I'd like to be able to display them in columns in a table like so: Code: [Select] Charne, Mr. Michael Glanger, Mrs. Karin Kling, Mr. Wayne Charne, Mrs. Suzette Glanger, Mr. Trevor Lazarow, Mrs. Fiona Charney, Mrs. Linda Jochelson, Mrs. Barbara Lazarow, Mr. Mark Charney, Mr. Norman Jochelson, Mr. Neil Norton, Mr. Charles Cohen, Mr. Brendan Karlan, Mr. Dennis Norton, Mrs. Jodi Cohen, Mrs. Joanna Karlan, Mrs. Helen Roy, Mr. Michael Flekser, Mrs. Jean Kling, Mrs. Danielle Roy, Mrs. Nicki Frysh, Dr. Howard Kling, Mrs. Melanie Tsafrir, Mrs. Lauren Frysh, Mrs. Sandra Kling, Mr. Nevil Tsafrir, Mr. Thomer That way it reads top to bottom THEN left to right. math-wise, it's simple to set up: Code: [Select] $num_results = number_of_$results; $numcols = 3; $numrows = trunc($numresults/numcols); <table> for row_loop=1 to numrows <tr> for col_loop = 1 to numcols <td> echo result(col_loop-1)*(numrows)+row_loop </td> next col_loop </tr> next row_loop </table> Anyone want to give this a shot? Revraz already directed me to http://www.phpfreaks.com/forums/index.php/topic,95426.0.html but that displayed the data from left to right then up to down like so: Code: [Select] Charne, Mr. Michael Charne, Mrs. Suzette Charney, Mrs. Linda Charney, Mr. Norman Cohen, Mr. Brendan Cohen, Mrs. Joanna Flekser, Mrs. Jean Frysh, Dr. Howard Frysh, Mrs. Sandra Glanger, Mrs. Karin Glanger, Mr. Trevor Jochelson, Mrs. Barbara Jochelson, Mr. Neil Karlan, Mr. Dennis Karlan, Mrs. Helen Kling, Mrs. Danielle Kling, Mrs. Melanie Kling, Mr. Nevil Kling, Mr. Wayne Lazarow, Mrs. Fiona Lazarow, Mr. Mark Norton, Mr. Charles Norton, Mrs. Jodi Roy, Mr. Michael Roy, Mrs. Nicki Tsafrir, Mrs. Lauren Tsafrir, Mr. Thomer It's a good temp solution, but if anyone is good with for loops, I'd appreciate the help, thanks! -Dave Hi, I am stuck. I have used dreamweaver to create a master details page with links to the details page successfully, but I would like to display the master details page results in 3 columns rather than the 1 column that dreamweaver defaults. As the site becomes richer the content will increase yielding many results, therfore the content displayed in a single column will require the users to scroll way down the page. So, as it is now the results display as follows: Plant1 Plant2 Plant3 Plant4 Plant5 Plant6 Plant7 Plant8 Plant9 etc... I would like them to display as follows: Plant1 Plant4 Plant7 Plant2 Plant5 Plant8 Plant3 Plant6 Plant9 Here is my code thus far: <?php require_once('Connections/connGND.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_connGND, $connGND); $query_rsBotanicalA = "SELECT * FROM plants WHERE CatIDB = 'A'"; $rsBotanicalA = mysql_query($query_rsBotanicalA, $connGND) or die(mysql_error()); $row_rsBotanicalA = mysql_fetch_assoc($rsBotanicalA); $totalRows_rsBotanicalA = mysql_num_rows($rsBotanicalA); ?> <table border="0" align="left"> <?php do { ?> <tr> <td align="left"><a href="plantdetails.php?recordID=<?php echo $row_rsBotanicalA['PlantID']; ?>"> <?php echo $row_rsBotanicalA['BotanicalName']; ?> </a></td> </tr> <?php } while ($row_rsBotanicalA = mysql_fetch_assoc($rsBotanicalA)); ?> </table> Could someone help me to write this code, as I have been unsuccessful maintaining the links. I have been able get the results to display in the columsn as I would like, but I haven't been able to figure it out while maintaining the links! Thanks. Hello freaks, Got a task here which is displaying correctly (I believe). I only have 3 data entries in the db right now. Like I said (I think) the display of the code below yields the right layout but it repeats the first db entry over and over. I got the display to work correctly like this: 1 2 3 4 5 6 7 8 9 I am trying to get this display result (so it is more eligable for the end user!) 1 4 7 2 5 8 3 6 9 Thanks in advance! CODE Code: [Select] <?php include_once "connect_to_mysql.php"; $cols = 3; $result = mysql_query("SELECT plantID, botanicalName FROM plants ORDER BY botanicalName"); $numrows = mysql_num_rows($result); $rows_per_col = ceil($numrows / $cols); $c = 1; $r = 1; while ($row = mysql_fetch_array($result)) { $plantID = $row["plantID"]; $botanicalName = $row["botanicalName"]; if ($r == $rows_per_col) { $c++; $r = 1; } else { $r++; } } $dyn_table = '<table width="750" cellpadding="0" cellspacing="0" border="0">'; for ($r = 1; $r <= $rows_per_col; $r++) { $dyn_table .= '<tr>'; for ($c = 1; $c <= $cols; $c++) { $dyn_table .= '<td><a href="plant_details.php?plantID = ' . $plantID . '" id="plantLink">' . $botanicalName . '</a></td>'; } $dyn_table .= '</tr>'; } $dyn_table .= '</table>'; ?> Hey all, First off, if this is in the wrong section I apologize. I wasn't sure if it should be here or the mySQL section. What's going on is, I'm in the process of learning the Prepared Statement way of doing things and am changing / updating my code to reflect the changes. Everything was going fine until I attempted to do what I could do using old MySQL methods and that is display the queried results on the same page. I can place a query and display the results as they should be displayed if I only use one block of code. However, if I try to do any additional queries on the same page, they get killed and do not display anything even though I know the query is fine because I can test the exact same syntax below one a different page and it works. Here's a code snippet for an example: Code: [Select] Code: <table> <tr> <td> // The below code will display a selection box containing various strings such as "hello world", "great to be here", "Wowserz", "this is mind blowing" etc. that are stored in the database. <?php echo "<select = \"SpecialConditions\">"; if($stmt->num_rows == NULL){ echo "No results found."; }else{ while($stmt->fetch()){ echo "<option value=\"$specialId\">$specialcondition</option>"; } } echo "</select>"; ?> </td> <td> // If I place another fetch query below the above fetch() query, this one will not show up. This one is supposed to display values 1 - 20 that have been stored in the DB. <?php echo "<select = \"NumberSets\">"; if($stmt->num_rows == NULL){ echo "No results found."; }else{ while($stmt->fetch()){ echo "<option value=\"".$numbers."\">".$numbers."</option>"; } } echo "</select>"; ?> </td> </tr> </table> What am I doing wrong with this? When I use regular SQL queries I can display multiple results on the same page. The results are being pulled from two separate joined tables but I don't think that's the issue. I have a search set up to search a table for the text entered in a textbox, I have two columns in the table, one with the first name of people, and the second with their last names, I am wondering how I can search both, so for instance: I type in the search field: Roger Smith in the database it would look like: First_name-----|-----Last_name -------------------|------------------- Roger------------|-------Smith my current query is: Code: [Select] $query = mysql_query("SELECT * FROM users WHERE fname LIKE '%$find%' OR lname LIKE '%$find%'"); But if I type both parts of the name it doesn't return anything. works fine if I just search for "Roger" OR "Smith". Hello, I have a quick question about methods for retrieving records from a mysql table and displaying them as a links For example, imagine I have three tables called countries, cities and city_info. I'd like to be able to select a country and have a list of that country's city names returned as links. I'd then like to be able to click on the link for London, say, and that would trigger a mysql query to retrieve the entry in city_info about London. Are there any functions that allow this? If anyone could point me in the right direction for further research I'd be grateful. Thanks. Hey guys, Having a slight problem with part of the code in my index.php file Code: [Select] mysql_select_db('db_name', $con); $result = mysql_query("SELECT * FROM spy ORDER BY id desc limit 25"); $resulto = mysql_query("SELECT * FROM spy ORDER BY id desc"); $count = mysql_num_rows($resulto); while($row = mysql_fetch_array($result)) { ?> <div class="contentDiv">Someone is looking at <?=$row[title];?> Stats for "<a href="/<?=$row[type];?>/<?=$row[code];?>/<?=$row[city];?>"><?=$row[code];?> <?=$row[city];?></a>"</div> <?}?> </div> <div id="login"></div> <? include("footer.php"); ?> </div> </body> </html> I'm getting the following error when viewing the file Code: [Select] Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in ~path to file/index.php on line 70 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in ~path to file/index.php on line 71 where lines 70 and 71 are $count = mysql_num_rows($resulto); while($row = mysql_fetch_array($result)) Any ideas on how to fix this? Hello everyone, So what I'm trying to do is have a dropdown menu displaying a number of <options> for people to select and to update that selection to the database, easy enough right? But I want that option to be displayed as the "selected" option when the page is revisited or refreshed and I just can't figure it out!!! (Permission to bang head on desk?) It would seem like it sould be a really basic thing to do but it's got me completely and a lot of menus around the site are going to rely on this so I came to you guys for help. A simple example would be like the facebook edit profile page, the user selects whether they are Male or Female, the database gets updated and when you return the option you selected before is the one that appears as if selected="selected" had been done. I've tried everything I can think of (all be it from a learners perspective) with no joy, ive managed to get the database connection sorted, the tables done, the login with unique id $_SESSION, logout etc... so then when I got to this I thought... easy LOL yeah right. Some of this probably doesnt even make sense but I'll show you the kind of things I've tried... <select name="gender" size="1" id="gender"> <option value="male" <?php if ($gender == "male") {echo 'selected="selected"';} ;?>>Male</option> <option value="female" <?php if ($gender == "female") {echo 'selected="selected"';} ;?>>Female</option> </select> OR <select name="gender" id="gender"> <option value="" selected="<?php if (!isset($gender)) {echo "selected";} ;?>">Select</option> <option value="male" selected="<?php if ($gender == "male") {echo "selected";} else {echo "";} ;?>">Male</option> <option value="female" selected="<?php if ($gender == "female") {echo "selected";} else {echo "";} ;?>">Female</option> </select> OR <select name="gender" size="1" id="gender"> <option selected="<?php if (!isset($gender)) {echo "selected";} ;?>">Select</option> <option value="<?php if ($gender == "Male") {echo "selected";} else {echo "male";} ;?>">Male</option> <option value="<?php if ($gender == "Female") {echo "selected";} else {echo "female";} ;?>">Female</option> </select> OR <select name="gender" id="gender"> <option value="male"><?php if ($gender == "male") {echo "Male";} ;?></option> <option value="female"><?php if ($gender == "female") {echo "Female";} ;?></option> </select> Honestly man, I've got no idea. The other thing is, I have more than 1 dropdown menu in the same form (5 in total) and if I use 2 or more selecting different options as I go I get a blank screen. And one more, if I have selected Male and it updates the users row and I resubmit Male again it's blank screen time again, lol. Any help would be tremendous and greatly appreciated. Thanks very much, Learner P.S Man! Hi All i am wanting a column list like this http://extensions.joomla.org/extensions basically the database is set up as (id, category, parent) I want the parent to group the category section and list like the the joomla example in three columns search various threads throughout the internet but none seem to cover this entirely can any one please help????? Hi Everyone, Well I have a bit of a problem... I have created a search page that I would like to return results from my MySQL database. However for some reason I can not get my page to display the results. Instead I am getting an error page. Below is the code I am using: In addition to this, should I wish for a user to be able to edit the returned results by clicking a link, how would I do that? <?php session_start(); ?> <link rel="stylesheet" type="text/css" href="css/layout.css"/> <html> <?php $record = $_POST['record']; echo "<p>Search results for: $record<br><BR>"; $host = "localhost"; $login_name = "root"; $password = "P@ssword"; //Connecting to MYSQL MySQL_connect("$host","$login_name","$password"); //Select the database we want to use mysql_select_db("schedules_2010") or die("Could not find database"); $result = mysql_query("SELECT * FROM schedule_september_2010 WHERE champ LIKE '%$record%' ") or die(mysql_error()); // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row echo "<br><p>Your Schedule<BR></p><br>"; echo "<table border=1>\n"; echo "<tr> <td bgcolor=#444444 align=center><p><b>Champ</p></td> <td bgcolor=#444444 align=center><p><b>Date</p></td> <td bgcolor=#444444 align=center><p><b>Start Time</p></td> <td bgcolor=#444444 align=center><p><b>End Time</p></td> <td bgcolor=#444444 align=center><p><b>Department</p></td> <td bgcolor=#444444 align=center><p><b>First Break</p></td> <td bgcolor=#444444 align=center><p><b>Second Break</p></td> <td bgcolor=#444444 align=center><p><b>Login ID</p></td> </tr>\n"; do { printf("<tr> <td><p>%s</p></td> <td><p>%s</p></td> <td><p>%s</p></td> <td><p>%s</p></td> <td><p>%s</p></td> <td><p>%s</p></td> <td><p>%s</p></td> <td><p>%s</p></td> </tr>\n" , $row["champ"] , $row["date_time"] , $row["start_time"] , $row["end_time"] , $row["department"] , $row["first_break"] , $row["second_break"] , $row["login_id"] ); } while ($row = mysql_fetch_array($result)); echo "</table>\n"; } else { echo "$champ No Records Found"; } mysql_free_result($result); mysql_close($con); ?> </html> Say a user puts in a support request, and for every request it generates a unqiue string, and enters it into the database. Ok, now say there is a text field, when the user enters their unique string and it finds a match, it displays the data along with it. How can I accomplish this? Im kind of new to mysql, but I know basic SQL. Would be great if somebody could point me in the right direction! Thanks Hi, I have a website where users can log on and edit their profile pic, name, biography etc. I was wondering about the correct way to:- Add data to the database through forms (Register.php) Display the data on a page Using mysql escape sting, however, the way I am currently using will display a '\' before any ' symbol. So it's >> it\'s ... Here is a snippet of the code I am using... Code: [Select] //insert data $about1 = mysql_real_escape_string($_POST['about']); //get $query = mysql_query("SELECT * FROM `staff` WHERE username='$username'"); $row = mysql_fetch_array($query); $about = $row['about']; echo $about; Not really sure how to get the images I have stored in MySQL into a html form. I can call-up the text fields from the database but it cannot seem to find the index for the images. Here is my code:- <?php session_start(); mysql_connect("localhost","root","abc") or die ("Error! Cannot connect to database"); mysql_select_db("theimageworks") or die ("Cannot find database"); $query = "SELECT * FROM jobs"; $result = mysql_query($query) or die (mysql_error()); ?> <?php //display data in html table echo "<table>"; echo "<tr><td>Username</td><td align='center'>Message</td><td>Product Image</td></tr>"; while($row = mysql_fetch_array($result)) { echo "</td><td>"; echo $row['username']; echo "</td><td>"; echo $row['message']; echo "</td></tr>"; echo $row['image']; } echo "</table>"; ?> The error message I get is "Notice: Undefined index: image in....." Thanks in advance! This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=308347.0 need a little help guys! I use the script below to display profile images, trouble is it shows 1 on top of the other, and i need it to double up 2 profile images on top of 2 profile images ect any ideas how i can do this. require("./include/mysqldb.php"); $con = mysql_connect("$dbhost","$dbuser","$dbpass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("$dbame", $con); $result = mysql_query("SELECT * FROM Search_profiles_up WHERE upgrade_one ='1' ORDER BY RAND() LIMIT 40"); print "<table width=\"293\" height=\"111\" border=\"0\"> <tr>\n"; while($row = mysql_fetch_array($result)) { print "<td width=\"142\"><img src=" . $row['search_small_image'] . " width=\"144\" height=\"169\" /></td>\n"; print " </tr>\n"; print " <tr> \n"; print "<td>" . $row['star'] . "</td>\n"; print " </tr>\n"; print " <tr>\n"; print "<td>" . $row['username_search'] . "</td>\n"; print " </tr>\n"; print " <tr> \n"; print "<td>" . $row['phone_search'] . "</td>\n"; print " </tr> \n"; } print "</table>"; mysql_close($con); ?> Need some help I have 2 tables in a database and I need to search the first table and use the results from that search, to search another table, can this be done? and if it can how would you recommend that I go about it? Thanks For Your Help Guys! Hello - hope you are all well.
I am creating a website for a family friend who has a shop. See preview of the site in this picture ....
Picture1.jpg 40.32KB
0 downloads
Currently the results are showing in one column. I want to show them in three columns in the format of:
1 2 3
4 5 6
7 8 9 etc
But formatted with the picture and then the title and the border.
The current code i have is....
HEAD
<?php
$sql = "SELECT * FROM abs_productcat WHERE catid = $catid ORDER BY catid ASC"; $can = mysql_query($sql); $catname = mysql_fetch_assoc($can); ?> BODY <?php while ($row_pro = mysql_fetch_assoc($rs_proddetails)) { ?><tr> <td><table width="243" border="0" align="left" cellpadding="15" cellspacing="0" class="sectionborders"> <tr> <td height="120" align="center"><p><strong><a href="indproducts.php?ProductID=<?php echo $row_pro['ProductID']; ?>"><img src="products/product_<?php echo $row_pro['ProductID']; ?>" alt="<?php echo $row_pro['ProductName']; ?>" height="120" class="sectionborders" border="0" /></a></strong></p> <p><strong><a href="indproducts.php?ProductID=<?php echo $row_pro['ProductID']; ?>" class="products"><?php echo $row_pro['ProductName']; ?></a></strong></p></td> </tr> </table></td> </tr> <tr> <td> </td> </tr> <?php } ?> THANK YOU FOR YOUR HELP!!! |