Jump to content

Recommended Posts

Hi guys

 

im new to PHP and im trying to pick up  as much as i can as quickly as possible

 

ive used a form to submit data to the database and now i want to pull that data and display it on a page.

 

Ive attached an image of what the final page will look like.

 

i just need to know how i will go about getttin that information out of the DB and onto the page in the way that i want it

 

i also want to display it in rows which have alternate colours, and also be able to click the buttons on left hand side to split the results into the appropriate months

 

hope you can help :)

 

displayug.jpg

hi guys sorry if im being annoying  but i am new and im trying to learn php really quickly to get page done but im definatley gettin confused now

 

im trying to display the information the way it is on the jpeg in the post above with alternate coloured cells

 

i was lookin online for a tutorial but im not gettin anywhere fast.. i did find one and did it but i doesnt quite fit with what i want to do, heres the code

 

<?php
$con = mysql_connect("localhost", "gezzamon_voting", "mrtickle");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("gezzamon_competition", $con);

$result = mysql_query("SELECT * FROM allymccoist");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['firstname'] . "</td>";
  echo "<td>" . $row['lastname'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>  

 

fingers crossed one of you guys can help

This is how you alternate colors

$i = 0; // initiate counter variable

// set the colors
$color1 = '#e1e1e1'; // first color
$color2 = '#cccccc'; // secound color

while($row = mysql_fetch_array($result))
  {
      // alternate row colors
      $bgcolor = ($i % 2 == 0) ? $color1 : $color2;
      
  echo "<tr style=\"background-color: $bgcolor\">"; // apply the color to the row
  echo "<td>" . $row['firstname'] . "</td>";
  echo "<td>" . $row['lastname'] . "</td>";
  echo "</tr>";
  
      $i++; // increment counter
  }

ahhhh

 

but what if ive made 2 classes in the css to style the cells? as they should have a border bottom and also padding?

 

also, how would i display the full line of text including varibles in the one cell? in the code i posted it only is 2 varibles posted into 2 cells.  I need the full sentence like the images i attached

 

thanks for ur help :)

ahhhh

 

but what if ive made 2 classes in the css to style the cells? as they should have a border bottom and also padding?

Then change

  echo "<tr style=\"background-color: $bgcolor\">"; // apply the color to the row

to

  echo "<tr class=\"$bgcolor\">"; // apply the class to the row

 

Now set the variables $color1 and $color2 to your two classes.

 

also, how would i display the full line of text including varibles in the one cell? in the code i posted it only is 2 varibles posted into 2 cells.  I need the full sentence like the images i attached

 

thanks for ur help :)

Just place the text after your variables.

You know how manually to assign a class to a tr right?

 

The trick that was shown to you is to use the modulus operator dividing by 2.  Do you know what a modulus does in math? 

 

Based on that trick, as you loop through the output of rows, you need to set the class either to be the even color or the odd color.  The modulus with the ternary operator that wildteen showed is how most people accomplish this.  The ternary operator is equivalent to an if-then-else, so based on the result of the modulus the variable is either going to be set to the 1st value if true, or the 2nd value if false. 

 

All you need to do is adapt the small piece of code that does the modulus and uses that in the ternary operator  (  (bool) ? value if true : value if false ) so that it sets the variable $bgcolor to be the name of the odd or even class.

 

All I can say at this point, is that if you have a specific question, or something specifically that you don't understand, then spell that out, preferably with code snippet included.

 

We understand that you are new, but we also expect people to try things out and read, and do their own explorations, otherwise we're just writing code for people, and we already do enough of that in our day jobs  ;)

<style type="text/css">

<!--

.style1 {

background:#CCCCCC;}

.style2 {

background:#cccccc;}

 

-->

</style>

 

<?

$con = mysql_connect("localhost", "gezzamon_voting", "mrtickle");

if (!$con)

{  die('Could not connect: ' . mysql_error());  }

 

mysql_select_db("gezzamon_competition", $con);

$result = mysql_query("SELECT * FROM allymccoist");

 

// set the class

$style1 = 'style1'; // first class 1

$style2 = 'style2'; // secound class 2

 

echo "<table border='1'><tr><th>Firstname</th><th>Lastname</th></tr>";

while($row = mysql_fetch_array($result)) 

// alternate row style     

$bgstyle = ($i % 2 == 0) ? $style1 : $style2;       

 

echo "<tr class=\"$bgstyle\">"; // apply the class to the row

echo "<td>" . $row['firstname'] . "</td>"; 

cho "<td>" . $row['lastname'] . "</td>"; 

echo "</tr>";

                                $ i++;

}

 

echo "</table>";

mysql_close($con);

?> 

 

Looks good.  You don't have to make string variables ($style1, $style2) just to use them in the ternary, you could just specify those same strings in the ternary.

 

$bgstyle = ($i % 2 == 0) ? 'style1' : 'style2';        

 

 

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.