PHP Development Board php divider

User Options
Register--Login--Top 20 Posters--Search Topics


Forum Main>>Tutorials>>Specifying how many columns per row when extractng from mysq

New Topic-Reply




Author
Post
Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged

PM ID and RPS ID: 1
PM [Chipmunk]

View Member Photo

Last replied to on Sun Nov 25, 2007 14:18:24
Edit Post|Quote
This is a simple tutorial that shows you how to make information or images(in this case buttons) display in a specific number columns per row using a specific algorithem and loop.

First for this example, create a table in mysql called affiliates with the follwing fields:

affiliateID -primary, auto-increment, bigint

URL - varchar, length 255

button - varchar length 255

URL will store the URL of the site you want to link to, and button will how the URL of the button of that site.

Now you need a simple connector file like the following connect.php to connect to the database.

Code:

<?
$db = mysql_connect("localhost", "username", "password") or die("Could not connect.");
if(!$db)
die("no db");
if(!mysql_select_db("database_name",$db))
die("No database selected.");
if(!get_magic_quotes_gpc())
{
$_GET = array_map('mysql_real_escape_string', $_GET);
$_POST = array_map('mysql_real_escape_string', $_POST);
$_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}
else
{
$_GET = array_map('stripslashes', $_GET);
$_POST = array_map('stripslashes', $_POST);
$_COOKIE = array_map('stripslashes', $_COOKIE);
$_GET = array_map('mysql_real_escape_string', $_GET);
$_POST = array_map('mysql_real_escape_string', $_POST);
$_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}
?>


Replace username, password, and database_name with your mySQL username,password, and database name.

Now populate the table with some data.

Then the actualy file to extract and put the data into a table is as follows:

Code:

<?PHP
include "connect.php"; //database connector here
print "<link rel='stylesheet' href='style.css' type='text/css'>";
$entriesperline=5;
$counter=1;
print "<table border='1'>";
$getbuttons="SELECT * from affiliates"; //query to get all affiliates
$getbuttons2=mysql_query($getbuttons) or die("Could not get buttons");
while($getbuttons3=mysql_fetch_array($getbuttons2))
{
if($counter%$entriesperline==1)
{
print "<tr><td><A href='$getbuttons3[URL]'><img
src='$getbuttons3[button]'></a></td>";
}
else if($counter%$entriesperline==0)
{
print "<td><A href='$getbuttons3[URL]'><img
src='$getbuttons3[button]'></a></td></tr>";
}
else
{
print "<td><A href='$getbuttons3[URL]'><img
src='$getbuttons3[button]'></a></td>";
}
$counter++;
} //exit loop
$counter--;
if($counter%$entriesperline!=0)
{
print "</tr>";
}
print "</table>";
?>

Entries per line is how many columns per row(in the case, how many buttons to display) per row to display in the table. Right now it is set to five, changing it to different number will cause the table to have different # of columns per row.

basically the way it is done is that the $counter variable tracks how many buttons it has displayed so far. Since $entriesperline is how many to display per row, when $counter is a multiple of $entriesperline plus 1, it should start a new row. This is exhibited by the
Code:

if($counter%$entriesperline==1)
{

line. The condition says if this condition happens then print <tr> which is the code for starting a new table.

Likewise if $counter is a multiple of $entriesperline, you have to end a row, so the next if statement:

Code:

else if($counter%$entriesperline==0)
{

The condition tells it to print a </tr>, which ends a table.

However, if there isn't a multiple of $entriesperline for total number of entries in a table, the row won't close. Thats what

Code:

$counter--;
if($counter%$entriesperline!=0)
{
print "</tr>";
}

is for. If the counter doesn't end on a table end, that line will end the table so as to finish what the loop left out.
-----------------------------
Chipmunk,
Supreme Administrator

itsjustdell
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 12202
[PM itsjustdell]

RPS score: 0
RPS challenge

Posted at Thu Apr 05, 2007 19:45:00
Edit post|Quote

am tryin to make this display 3 column per row, what am i missing or what am i doing wrong?


 


<? $en['lp_ind'] = 0; ?><!--[Loop Start QUERY:SELECT *,q_name_<%lang%> AS q_name FROM $questtable WHERE q_type=4 ORDER BY q_order ASC]--><? if ($en['lp_ind'] == 0) $en['lp_ind'] = $en['loops_left']+1; $en['lp_ind']--;?><!--[Loop Start QUERY:SELECT * FROM $membtable LEFT JOIN $picstable ON i_user=m_id LEFT JOIN $onlinetable ON o_id=m_id WHERE i_status=2 AND m_confirmed>0 ORDER BY `m_date` DESC LIMIT 100]--><? $en['ftr']++; ?><!--[If Start $en['ftr'] == 1]--><!--[If End]--><!--[If Start $en['loops_index'] == 1]--><!--[If End]--><table border="0" cellpadding="0" cellspacing="4" bgcolor="#FFFFFF" style="margin:3; border:1px dotted #C0C0C0; " width="205"><tr><td rowspan="3" width="65"><a href='<%dir%>view/<%m_user%>.html'><img src='<%dir%>images/pictures/<%m_id%>-<%i_id%>.jpg' width="55" height="55" style="border: 5px solid #c0c8d3;"></a></td><td align="right" width="45" valign="bottom"><font color="000080">name :</font></td><td valign="bottom" align="left" width="128"><a href='<%dir%>view/<%m_user%>.html'><%m_user%></a></td></tr><tr><td align="right" width="45"><font color="000080">age :</font></td><td align="left" width="128"><? $en['mm_year'] = $en['m_year']; $en['mm_month'] = $en['m_month']; $en['mm_day'] = $en['m_day']; echo age(); ?></td></tr><tr><td colspan="2" valign="middle" style="border-top: 1px dotted #C0C0C0"><p align="center"><a href="<%dir%>index.php?req=compose&to=<%m_user%>">send message</a></td></tr></table><!--[If Start $en['loops_left'] == 0]--><!--[If End]--><!--[Loop End]--><!--[Loop End]--><!--[If Start $en['ftr'] > 0]--><!--[If End]-->



Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Thu Apr 05, 2007 20:06:37
Edit post|Quote
Please use the [.code] and [./code] tags without the . when posting code, I can't read what you just wrote.
-----------------------------
Chipmunk,
Supreme Administrator

2Gun
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 14260
[PM 2Gun]

RPS score: 0
RPS challenge

Posted at Sat Nov 24, 2007 19:14:32
Edit post|Quote
Chipmunk ...Ive been reviewing your code for displaying images and limiting the number of images per row but havent had any luck getting this to work. If you wouldnt mind ill paste my base source snippet for a image gallery/product list to show you the core for it.
Code:
      echo"<table width='555' border='0' align='center' cellpadding='0' cellspacing='0'>      <tr>            <td align='center' valign='top'>";            if ($_GET[gameid] > 0) {            $query = mysql_query("SELECT * FROM gamelist WHERE id='$_GET[gameid]'");        while ($row = mysql_fetch_array($query)) {    $filelist = mysql_query("SELECT * FROM files WHERE game='$row[image]' ORDER BY date ASC LIMIT $from, $max_results", $db);    $getcount = mysql_query("SELECT * FROM files WHERE game='$row[image]'", $db);            $total_results = mysql_result($getcount, 0);            $total_pages = ceil($total_results / $max_results);            $fr = $from + 1;            $to = $from + mysql_num_rows($filelist);             while ($file = mysql_fetch_array($filelist)) {          echo"<img src='$file[link]' width='175' border='0'>";            echo"</td></tr>";        }    echo"</table>";
As you can see right now I have it just displaying the images in a single column which displays them fine as 3 across put there unformatted and not aligned at all which is impossible unless I can do what you did above.Any help would be greatly appreciated and thank you for your time.

Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Sun Nov 25, 2007 14:18:24
Edit post|Quote
Where's your counter for how many columns per row it currently has?
-----------------------------
Chipmunk,
Supreme Administrator

Page: 1



Powered by © Chipmunk Board

Flash games Ninja games-Web Design New York