Drewdle Posted January 18, 2011 Share Posted January 18, 2011 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.... Quote Link to comment https://forums.phpfreaks.com/topic/224813-splitting-mysql-results/ Share on other sites More sharing options...
trq Posted January 18, 2011 Share Posted January 18, 2011 See http://www.phpfreaks.com/forums/faqcode-snippet-repository/multi-column-results Quote Link to comment https://forums.phpfreaks.com/topic/224813-splitting-mysql-results/#findComment-1161236 Share on other sites More sharing options...
Drewdle Posted January 18, 2011 Author Share Posted January 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224813-splitting-mysql-results/#findComment-1161242 Share on other sites More sharing options...
trq Posted January 18, 2011 Share Posted January 18, 2011 That is a mysql error. It's refering to line 1 of your query. Can we see the other query and surrounding code? Quote Link to comment https://forums.phpfreaks.com/topic/224813-splitting-mysql-results/#findComment-1161245 Share on other sites More sharing options...
Drewdle Posted January 18, 2011 Author Share Posted January 18, 2011 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/224813-splitting-mysql-results/#findComment-1161247 Share on other sites More sharing options...
trq Posted January 18, 2011 Share Posted January 18, 2011 Niether of those queries could produce the error described. Is there more code? Quote Link to comment https://forums.phpfreaks.com/topic/224813-splitting-mysql-results/#findComment-1161250 Share on other sites More sharing options...
Drewdle Posted January 18, 2011 Author Share Posted January 18, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/224813-splitting-mysql-results/#findComment-1161255 Share on other sites More sharing options...
dragon_sa Posted January 18, 2011 Share Posted January 18, 2011 one problem is this while($row1 = mysql_fetch_array($result)) should be while($row1 = mysql_fetch_array($result1)) Quote Link to comment https://forums.phpfreaks.com/topic/224813-splitting-mysql-results/#findComment-1161256 Share on other sites More sharing options...
trq Posted January 18, 2011 Share Posted January 18, 2011 one problem is this while($row1 = mysql_fetch_array($result)) should be while($row1 = mysql_fetch_array($result1)) That'll be it. I missed that all together. You can re-use variables you know, no real need for $result, $result1, $result2 etc etc. Quote Link to comment https://forums.phpfreaks.com/topic/224813-splitting-mysql-results/#findComment-1161258 Share on other sites More sharing options...
Drewdle Posted January 18, 2011 Author Share Posted January 18, 2011 @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! Quote Link to comment https://forums.phpfreaks.com/topic/224813-splitting-mysql-results/#findComment-1161260 Share on other sites More sharing options...
trq Posted January 18, 2011 Share Posted January 18, 2011 You need to save your queries into a variable then echo them. The error is indicating that your are passing a resource into it somewhere. Again, none of the code you have posted would create that error. Quote Link to comment https://forums.phpfreaks.com/topic/224813-splitting-mysql-results/#findComment-1161264 Share on other sites More sharing options...
dragon_sa Posted January 18, 2011 Share Posted January 18, 2011 also this echo "<td><b>Username: <? echo ['Username'] ?> <br><b>Group:</b><? echo ['Level'] ?><hr></td>"; should be echo "<td><b>Username: ".$row1['Username']."<br><b>Group:</b>".$row1['Level']."<hr></td>"; Quote Link to comment https://forums.phpfreaks.com/topic/224813-splitting-mysql-results/#findComment-1161267 Share on other sites More sharing options...
dragon_sa Posted January 18, 2011 Share Posted January 18, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/224813-splitting-mysql-results/#findComment-1161269 Share on other sites More sharing options...
trq Posted January 18, 2011 Share Posted January 18, 2011 Ha, I'm on fire tonight. Missing stuff all over the place. Change.... <?php $query1 = mysql_query('SELECT * FROM users ORDER BY Username'); ?> to.... <?php $query1 = 'SELECT * FROM users ORDER BY Username'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/224813-splitting-mysql-results/#findComment-1161272 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.