Jump to content

easy stuff (long time with out coding)


aaronrb

Recommended Posts

Hi Guys,

been a while since coding in php,

i am trying to pull information out of a database, and loop it in a table so it displays a new set of information per cell.

there will be 2 or 3 cells per row and as many rows as need.

 

i have the information coming out of the db OK.

when it puts it into two rows, it shows the same data, i know i have put the </tr> in the wrong place, but i cant work out where to put it.

 

<body>

<table width="95%" border="1">

 

<?

require 'db_connect.php';

$query = "SELECT * FROM tkd_members where status = 'Active' ORDER BY lastname ASC";

$result = mysql_query($query) or die("Error" . mysql_error());

 

    while ($row = mysql_fetch_array($result))

{

    $counter = 1;

   

 

        ?>

    <tr>

        <td>

<?

  echo $row['firstname'] . " ". $row['midname'] . " " . $row['lastname'] . "<br>";

echo $row['address'] . " " . $row['address2'] . " <br>";

echo $row['town'] . " " . $row['county'] . "<br>";

echo $row['postcode'] . "<br>";

echo $row['telephone'] . "  /  " . $row['mobile'] . "<br>";

echo $row['contactname1'] . " " . $row['contactnumber1'] . "<br>";

echo $row['contactname2'] . " " . $row['contactnumber2'] . "<br>";

 

echo "DOB:  " . $row['dob'];

        $counter++

        ?>     

        </td>

        <td>

<?

echo $row['firstname'] . " ". $row['midname'] . " " . $row['lastname'] . "<br>";

echo $row['address'] . " " . $row['address2'] . " <br>";

echo $row['town'] . " " . $row['county'] . "<br>";

echo $row['postcode'] . "<br>";

echo $row['telephone'] . "  /  " . $row['mobile'] . "<br>";

echo $row['contactname1'] . " " . $row['contactnumber1'] . "<br>";

echo $row['contactname2'] . " " . $row['contactnumber2'] . "<br>";

 

echo "DOB:  " . $row['dob'];

        $counter++

        ?>     

        </td>

 

       

<?

    }

    ?>

</tr>

</table>

</body>

 

i cant show you the actual output page, as it has details of my studetns on it. but i think i have explained it enough.

 

i appreciate the help so if you dont get what i mean, please contact me.

 

Regards

Link to comment
Share on other sites

Aaron,

This is how i loop the database results. I plugged in some of your database fields to give you a better understanding. You can also change the color of the alternating rows or just change them both to white (#FFFFFF). Also when the results are being echo'd out you can change the structure of the table for example:


<td><p class=info>' .$results['firstname'] . '<br>
' .$results['address'] . ' ' . $results['address2'] . '</p></td> 

 

Hope this helps if not hope you find what you are looking for.

 

 

</p></td>

 

 

 

<?php   
					   //Setup connection to the database 
					   $connect = mysql_pconnect("localhost", "yourusername", "yourpassword") 
					   or die(mysql_error()); 

					   //Connect to the database 
						mysql_select_db("yourdatabasename", $connect) or die(mysql_error());    

					   // Perform an SQL query on the Address table    
						$sql_address = mysql_query("SELECT firstname, midname, lastname, address, address2, town, county, postcode, telephone, mobile, contactname1, contactnumber1") 
					   or die (mysql_error()); 
						   
						// Define the colours for the alternating rows 
						$colour_odd = "#FDFDEA"; 
						$colour_even = "#E0E0C7"; 
					   $row_count = 0;  //To keep track of row number 

					   // Fetch the array of records and Loop through them 
						while($results = mysql_fetch_array($sql_address)) 
					   { 
						 // Decide which colours to alternate for the rows 
						 // If Remainder of $row_count divided by 2 == 0. 
						 $row_color = (($row_count % 2) == 0) ? $colour_even : $colour_odd; 

						 /* Echo the table row and table data that needs to be looped Check All  /  Uncheck All */

						 //With selected: over until the end of the recordset     
						 echo ' 
						 <tr bgcolor="' . $row_color . '"> 
						  <td><p class=info>' .$results['firstname'] . '</p></td> 
						  <td><p class=info>' .$results['midname'] . '</p></td> 
						  <td><p class=info>' .$results['lastname'] . '</p></td>
						  <td><p class=info>' .$results['address'] . '</p></td>
						  <td><p class=info>' .$results['address2'] . ' ' . $results['town'] . '</p></td>
						  <td><p class=info>' .$results['county'] . '</p></td>
						  </tr>'; 

						 // Increment the row count 
						 $row_count++; 
					   } 
					   // Free the MySQL resource 
					   mysql_free_result($sql_address); 
					   // Close the database connection 
					   mysql_close($connect); 
					   ?> 

Link to comment
Share on other sites

hey aeafisme23,

ya know thats not exactly what i was after, but its great and does the job.

 

tHANK you so much....

 

just one small query, how would i be able to do something similiar to this,

 

Student 1's details | Student 2's Details

-------------------------------------------------------

Student 3's Details | Student 4's Details

 

if you can imagine that as a table..

 

Thanks again...

Link to comment
Share on other sites

What I would do, firstly, take the variable $counter = 1 out side of the while function (else it will be the same everytime), then change is value to 0, in the first few lines of the while function place $counter++;.

 

To achieve what you want to, I would add an if into your while, catch my drift?

 

$counter = '0';

while($something is going on) {

$counter++;

if($counter = $counter%2) {

echo '<tr><td>';

        echo $row['firstname'] . " ". $row['midname'] . " " . $row['lastname'] . "

";

      echo $row['address'] . " " . $row['address2'] . "

";

      echo $row['town'] . " " . $row['county'] . "

";

      echo $row['postcode'] . "

";

      echo $row['telephone'] . "  /  " . $row['mobile'] . "

";

      echo $row['contactname1'] . " " . $row['contactnumber1'] . "

";

      echo $row['contactname2'] . " " . $row['contactnumber2'] . "

";

     

      echo "DOB:  " . $row['dob'];

echo '</td>';

}

else {

echo '<td>'

      echo $row['firstname'] . " ". $row['midname'] . " " . $row['lastname'] . "

";

      echo $row['address'] . " " . $row['address2'] . "

";

      echo $row['town'] . " " . $row['county'] . "

";

      echo $row['postcode'] . "

";

      echo $row['telephone'] . "  /  " . $row['mobile'] . "

";

      echo $row['contactname1'] . " " . $row['contactnumber1'] . "

";

      echo $row['contactname2'] . " " . $row['contactnumber2'] . "

";

      echo "DOB:  " . $row['dob'];

echo '</td></tr>';

}

}

 

Just fiddle it around slightly, but that will get your

 

Student 1's details | Student 2's Details

-------------------------------------------------------

Student 3's Details | Student 4's Details

 

style table

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.