Jump to content

Splitting MySql results?


Drewdle

Recommended Posts

I have a problem displaying the results of a table,

 

It shows them in one long list and I'd like them to show as 2 or 3.

 

I had a look around and the most favoured way to this I found was to split the results either by odd/even or using percentages but Im having trouble implementing it into my script.

 

How would I go about adding the odd/even way to this:

<?php
$query = mysql_query('SELECT * FROM users ORDER BY Username');
while ($row = mysql_fetch_array($query)) {
if ($row['UserID']) 
       ?>
<b>Username:</b> <span class=class1><a href="aeditprofile.php?username=<? echo $row['Username'] ?>"><? echo $row['Username'] ?></a></span><br>
<b>Group:</b> <? echo $row['Level'] ?></color><br /><hr>
<?php
}
?>

 

Essentially I'm after getting something like this:

 

Name                          Name                          Name

Group                          Group                        Group

 

and so on down the page....

Link to comment
https://forums.phpfreaks.com/topic/224813-splitting-mysql-results/
Share on other sites

Tried that:

<?php
$query1 = mysql_query('SELECT * FROM users ORDER BY Username');
?> 
<table cellspacing="3" cellpadding="3">
<?php
$result1 = mysql_query($query1) or die("There was a problem with the SQL query: " . mysql_error()); 
if($result1 && mysql_num_rows($result1) > 0)
{
    $i = 0;
    $max_columns = 3;
    while($row1 = mysql_fetch_array($result))        
   {
       // make the variables easy to deal with
       extract($row1);

       // open row if counter is zero
       if($i == 0)
          echo "<tr>";

       // make sure we have a valid product
       if($row1['Username'] != "" && $row1['Username'] != null)
          echo "<td><b>Username: <? echo ['Username'] ?> <br><b>Group:</b><? echo ['Level'] ?><hr></td>";
    
       // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
   } // end while
} // end if results

// clean up table - makes your code valid!
if($i > 0)
{
    for($j=$i; $j<$max_columns;$j++) echo "<td> </td>";
   echo '</tr>';
}
?>
</table>
?>

 

And got:

 

There was a problem with the SQL query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #6' at line 1

 

Line 1 is an include that includes a file which also runs a query.

Excuse the mess its in, been doing php for about a week, its all trial and error learning atm!

 

<?php  
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))  
{  
     ?>

Welcome, <?=$_SESSION['Username'] ?>!<br>


     <?php  
}  
elseif(!empty($_POST['username']) && !empty($_POST['password']))  
{  
    $username = mysql_real_escape_string($_POST['username']);  
    $password = md5(mysql_real_escape_string($_POST['password']));  
  
    $checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");  
  
    if(mysql_num_rows($checklogin) == 1)  
    {  
        $row = mysql_fetch_array($checklogin);  
        $email = $row['EmailAddress'];  
  
        $_SESSION['Username'] = $username;  
        $_SESSION['EmailAddress'] = $email;  
        $_SESSION['LoggedIn'] = 1;  
  
        echo "Success!";    
        echo "<script type=text/javascript>
// The time out value is set to be 10,000 milli-seconds (or 10 seconds)
setTimeout(' document.location=document.location' ,2000);
</script>";  
    }  
    else  
    {  
?>
<meta http-equiv="refresh" content="2;index.php">
Error: Account does not exist.
<?php 
        
    }  
}  
else  
{  
    ?>  
<form method="post" action="index.php" name="loginform" id="loginform">
<b>Username:</b> <input name="username" type="text" id="username" class="box">
<b>Password:</b> <input type="password" name="password" id="password" class="box">
<input type="submit" name="login" id="login" class=btn value=Login>
</form>
<?php  
}  
?>  

Nope, just an included nav and footer but they dont contain any php? :l

 

Well no queries anyway...

 

Nav:

<table width=100% border=0 cellspacing=5 cellpadding=5>
<tr>
<td width=160 valign=top>
<div class=nav>
<b>Navigation</b><br><br>
<span class=class1><a href=index.php>Main</a></span><br>
<span class=class1><a href=about.php>About</a></span><br>
<span class=class1><a href=faq.php>FAQ</a></span><br>
<span class=class1><a href=contact.php>Contact</a></span>
</div>
<br><br>
<?php  
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))  
{  
     ?>
<div class=nav>
<b>User Navigation</b><br><br>
<span class=class1><a href=viewprofile.php?username=<? echo $_SESSION['Username']?>>Your Profile</a></span><br>
<span class=class1><a href=editprofile.php?username=<? echo $_SESSION['Username']?>>Edit Your Profile</a></span><br><br>
<span class=class1><a href=viewmembers.php>Member List</a></span><br> 
<span class=class1><a href=logout.php>Logout</a></span><br><br>
<span class=class1><a href=admin.php>Admin?</a></span><br>
</div>
<?php
}  
else  
{  
?>
<div class=nav>
<b>User Navigation</b><br><br>
<span class=class1><a href=register.php>Register</a></span><br>
</div>
<?php
}  
?>

</td>
<td valign=top>
<center>

@dragon_sa

 

Cheers for the mistake find, changed it!

 

@thorpe

 

I know, but with it being in a separate file I forget what variables I've set :l Lol.

When its done I intend to re-create but with variables all being set in one file and including it.

 

However I still get the Resource #6 error!

 

also I think this is you problem

 

<?php
$query1 = mysql_query('SELECT * FROM users ORDER BY Username');
?> 
<table cellspacing="3" cellpadding="3">
<?php
$result1 = mysql_query($query1) or die("There was a problem with the SQL query: " . mysql_error()); 

 

should be

 

<?php
$query1 = mysql_query('SELECT * FROM users ORDER BY Username') or die("There was a problem with the SQL query: " . mysql_error());
?> 
<table cellspacing="3" cellpadding="3">
<?php
if($query1 && mysql_num_rows($query1) > 0)

you are trying to perform a query on a query

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.