Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,180 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,145 people online right now. Registration is fast and FREE... Join Now!




PHP and MYSQL - print 2 columns

2 Pages V  1 2 >  
Reply to this topicStart new topic

PHP and MYSQL - print 2 columns

kruenb14
31 Aug, 2008 - 08:53 AM
Post #1

New D.I.C Head
*

Joined: 25 Apr, 2008
Posts: 8

HI!

I'm working on a project for my portfolio...the class that I learned PHP in had a database that we were able to use an odbc connection, but now my web host only has sql databases, so I'm doing a crash course in learning sql!

I have pretty much figured things out, but my original code had the information printing out in two columns, based on if the set db ID# was odd or even, but I can't seem to get that function working again, and I've tried everything, but it is just stumping me!!! Any suggestions is very much appreciated!!!

CODE

$sql = "SELECT * FROM Directors ";

$myrecordset = mysql_query($sql, $connection) or die ("Could not execute sql: $sql");


?>

<head>


<title>Best Movie Directors</title>
<link href="style.css" rel="stylesheet" type="text/css">


</head>

<body>

<div class="container">

<img src="images/banner.jpg" alt="banner">
<br /><br /><br /><br />

<?



echo "<table align=\"center\">";

while($row = mysql_fetch_array($myrecordset))


    {
        if ($row['DirectorID'] % 2)
        
        {
    
            print "<tr><td><a href= \"director.php?DirectorID=".$row['DirectorID']."\">".$row['ButtonUp']."</a></td>";
        
        }
        
        else
        
        {
        
            print "<tr><td><a href= \"director.php?DirectorID=".$row['DirectorID']."\">".$row['ButtonUp']."</a></td></tr>";
            
        }
}
    
echo "</table>";


?>
</div>

</body>
</html>


Thanks!
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: PHP And MYSQL - Print 2 Columns
31 Aug, 2008 - 09:11 AM
Post #2

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 481



Thanked: 32 times
My Contributions
You might need to assign $row['DirectorID'] to another variable and test that:
$x=$row['DirectorID'];
if ($x%2){



User is offlineProfile CardPM
+Quote Post

kruenb14
RE: PHP And MYSQL - Print 2 Columns
31 Aug, 2008 - 09:18 AM
Post #3

New D.I.C Head
*

Joined: 25 Apr, 2008
Posts: 8

Ok, this may be a stupid question, but where would I assign it? I've tried to assign variables, but it doesn't seem to do anything. Do I do it in the "while" statement?
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: PHP And MYSQL - Print 2 Columns
31 Aug, 2008 - 09:43 AM
Post #4

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
nope... you have $row['DirectorID']%2

that will ALWAYS be true because you never compare it to anything... you're basically saying if it can be divided by 2, and everything can

so replace it with this:
php
if($row['DirectorID']%2 == 0){}
else{}


This post has been edited by JBrace1990: 31 Aug, 2008 - 09:44 AM
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: PHP And MYSQL - Print 2 Columns
31 Aug, 2008 - 10:16 AM
Post #5

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 481



Thanked: 32 times
My Contributions
blink.gif Oops! Sorry I missed that.
User is offlineProfile CardPM
+Quote Post

kruenb14
RE: PHP And MYSQL - Print 2 Columns
31 Aug, 2008 - 10:26 AM
Post #6

New D.I.C Head
*

Joined: 25 Apr, 2008
Posts: 8

That makes sense...but I tried it and it didn't do anything....
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: PHP And MYSQL - Print 2 Columns
31 Aug, 2008 - 11:44 AM
Post #7

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 481



Thanked: 32 times
My Contributions
Try it in a test php file:
CODE
$num=4; // try various odd/even whole numbers.
if ($num%2 == 0){
echo "even<br>";
} else {
echo "odd<br>";
}

If that works then you have to wonder if it's getting to that part of your script.
User is offlineProfile CardPM
+Quote Post

kruenb14
RE: PHP And MYSQL - Print 2 Columns
31 Aug, 2008 - 11:56 AM
Post #8

New D.I.C Head
*

Joined: 25 Apr, 2008
Posts: 8

The test script does work. I'm thinking that it has something to do with calling up the "DirectorID" field. I'm not sure why that isn't working...
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: PHP And MYSQL - Print 2 Columns
31 Aug, 2008 - 11:59 AM
Post #9

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 481



Thanked: 32 times
My Contributions
QUOTE(kruenb14 @ 31 Aug, 2008 - 12:56 PM) *
I'm thinking that it has something to do with calling up the "DirectorID" field.

Which brings me back to my original thought:

You might need to assign $row['DirectorID'] to another variable and test that:
$x=$row['DirectorID'];
// or possibly:
$x=(int)$row['DirectorID'];

if ($x%2 == 0){

This post has been edited by CTphpnwb: 31 Aug, 2008 - 12:00 PM
User is offlineProfile CardPM
+Quote Post

kruenb14
RE: PHP And MYSQL - Print 2 Columns
31 Aug, 2008 - 12:16 PM
Post #10

New D.I.C Head
*

Joined: 25 Apr, 2008
Posts: 8

I've tried assigning variables in this code, and it doesn't do anything, I'm not sure why.

That's why I asked where about in the code that I should put it...it doesn't seem to do anything no matter where I put it....

If I leave the if else statement out, it prints things out fine, just in one column....
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: PHP And MYSQL - Print 2 Columns
31 Aug, 2008 - 12:22 PM
Post #11

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 481



Thanked: 32 times
My Contributions
Show us the code as you have it now, with the if statement.
User is offlineProfile CardPM
+Quote Post

kruenb14
RE: PHP And MYSQL - Print 2 Columns
31 Aug, 2008 - 12:26 PM
Post #12

New D.I.C Head
*

Joined: 25 Apr, 2008
Posts: 8

CODE
mysql_select_db($db, $connection) or die ("Could not open $db database");

$sql = "SELECT * FROM Directors ";

$myrecordset = mysql_query($sql, $connection) or die ("Could not execute sql: $sql");


?>

<head>


<title>Best Movie Directors</title>
<link href="style.css" rel="stylesheet" type="text/css">


</head>

<body>

<div class="container">

<img src="images/banner.jpg" alt="banner">
<br /><br /><br /><br />

<?



echo "<table align=\"center\">";

while($row = mysql_fetch_array($myrecordset))

    {
        if ($row['DirectorID']%2 == 0)
        
        {
    
            print "<tr><td>".$row['ButtonUp']."</td>";
        
        }
        
        else
        
        {
        
            print "<tr><td>".$row['ButtonUp']."</td></tr>";
            
        }

    }
    
echo "</table>";


?>
</div>

</body>


This post has been edited by kruenb14: 31 Aug, 2008 - 12:47 PM
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:47AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month