TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   I need of some help here (http://www.talkphp.com/absolute-beginners/3031-i-need-some-help-here.html)

codefreek 06-30-2008 01:54 PM

I need of some help here
 
Sorry i misspell the topic name xD

Is the problem in this script
that i am not calling out the news part
with
PHP Code:

while($website_row mysql_fetch_array($web_result))
{
print 
$website_row['rest'];


? or something else it just wont work :S

It is suposed to switch around to news cat 1 news cat 2
so all the post with cat id 1 gets that in cat 1 etc..

it worked with my links but not on this so whats
wrong ??..

Thank you in advance




PHP Code:

<?php
include('db_connect.php');
$webq "select id, name, des from news ";
if(isset(
$_GET['cat']))
 {
   
$webq .= sprintf("where cat_id = '%s'"mysql_real_escape_string($_GET['cat']));
 }
 
$web_result mysql_query($webq);
 if(!
cat_result)
{
echo 
mysql_error();
}
$catq "select id, name from cat";
$cat_result mysql_query($catq);
 
?>
 
<?php
while($cat_row mysql_fetch_array($cat_result))
{
?>
<a href="test.php?cat=<?php echo $cat_row['id'];?>"><?php echo $cat_row['name'];?></a>
<?php
}
?>





My cat table,

Code:

CREATE TABLE `cat` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Data in table `cat`
--

INSERT INTO `cat` (`id`, `name`) VALUES
(1, 'news one'),
(2, 'news two');



my news table

Code:

CREATE TABLE `news` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL,
  `des` varchar(150) NOT NULL,
  `rest` text NOT NULL,
  `tim` date NOT NULL,
  `valid` int(11) NOT NULL,
  `users_id` int(11) NOT NULL,
  `cat_id` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

--
-- Data in table `news`
--

INSERT INTO `news` (`id`, `name`, `des`, `rest`, `tim`, `valid`, `users_id`, `cat_id`) VALUES
(1, 'lol', 'omg this is the edit title', '<p>Lorem ipsum dolor sit amet beatae iste aperiam vitae magni aperiam omnis omnis, doloremque ut odit illo aperiam inventore inventore illo natus unde voluptas consequuntur aperiam sunt dicta iste dolores eos aspernatur aspernatur, perspiciatis dicta aut sit </p>\r\n<p>Lorem ipsum dolor sit amet beatae iste aperiam vitae magni aperiam omnis omnis, doloremque ut odit illo aperiam inventore inventore illo natus unde voluptas consequuntur aperiam sunt dicta iste dolores eos aspernatur aspernatur, perspiciatis dicta aut sit </p>\r\n<p>Lorem ipsum dolor sit amet beatae iste aperiam vitae magni aperiam omnis omnis, doloremque ut odit illo aperiam inventore inventore illo natus unde voluptas consequuntur aperiam sunt dicta iste dolores eos aspernatur aspernatur, perspiciatis dicta aut sit </p>', '2008-06-22', 1, 0, 1),
(5, 'admin', 'hello', '<p>Lorem ipsum dolor sit amet beatae iste aperiam vitae magni aperiam omnis omnis, doloremque ut odit illo aperiam inventore inventore illo natus unde voluptas consequuntur aperiam sunt dicta iste dolores eos aspernatur aspernatur, perspiciatis dicta aut sit </p>\r\n<p>Lorem ipsum dolor sit amet beatae iste aperiam vitae magni aperiam omnis omnis, doloremque ut odit illo aperiam inventore inventore illo natus unde voluptas consequuntur aperiam sunt dicta iste dolores eos aspernatur aspernatur, perspiciatis dicta aut sit </p>\r\n', '2008-06-22', 1, 0, 1),
(6, 'roland', 'hello sir', 'mertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia', '2008-06-28', 1, 0, 2);

my users table

Code:

CREATE TABLE `users` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(30) NOT NULL,
  `password` varchar(40) NOT NULL,
  `email` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;

--
-- Data in table `users`
--

INSERT INTO `users` (`id`, `username`, `password`, `email`) VALUES
(1, 'user', 'password', 'lol@info.com'),

Thank you!

codefreek 06-30-2008 02:10 PM

sql table in pastebin!
PasteBin.be
code in pastebin
PasteBin.be

codefreek 06-30-2008 04:24 PM

i found a bug i had 2 "where" changed it to and but still not working
PHP Code:

$webq "select id, name, des from news valid = 1 ";
if(isset(
$_GET['cat']))
 {
   
$webq .= sprintf("and cat_id = '%s'"mysql_real_escape_string($_GET['cat']));
 } 


codefreek 06-30-2008 04:40 PM

this is the complete code
PasteBin.be

codefreek 06-30-2008 06:50 PM

like 100 php Guru's here and no one can help me? :S

webid 06-30-2008 06:58 PM

while($website_row = mysql_fetch_array($web_result))

check $web_result before that line and post it here please.

---

sorry, $cat_result

codefreek 06-30-2008 07:24 PM

PHP Code:

<style type="text/css">
.style4 {
    font-size: xx-large;
    color: #FF9900;
}
</style>
<?php
error_reporting
(E_ALL & ~E_NOTICE);
include(
"db_connect.php");
session_start(); // Starts the session.

if ($_SESSION[‘logged’] != 1) { // There was no session found!

        
header("Location: users.php"); // Goes to login page.

        
exit(); // Stops the rest of the script.

}

echo 
"This is the main page!";


$res "SELECT `id`, `name`, `des`, `rest`, `tim` FROM `news` WHERE `valid` = 1";

$web_result mysql_query($res);

if(!
$web_result) {
echo 
mysql_error();
}

else {
   
}


?>
<?php 

 
while($website_row mysql_fetch_array($web_result))
{


?>

<font size='3' color='CC0000'><strong>
<h1 class="style4">
<?php print $website_row['des'];?></br>
</h1>
</strong></font>


<table border = "0">
<tr>
<th>NEWS:</th>
<td>
<?php print $website_row['rest']; ?>
</div>

</td>

</tr>
</table>


<?php ?>
<?php
echo "<a href=\"logout.php\">Logout</a>";
?>




<?php
$webq 
"select id, name, des from news where valid = 1 ";
if(isset(
$_GET['cat']))
 {
   
$webq .= sprintf("and cat_id = '%s'"mysql_real_escape_string($_GET['cat']));
 }
 
$web_result mysql_query($webq);
 if(!
$cat_result)
{
echo 
mysql_error();
}
$catq "select id, name from cat";
$cat_result mysql_query($catq);

?>

<?php
while($cat_row mysql_fetch_array($cat_result))
{
?>
<a href="index.php?cat=<?php echo $cat_row['id'];?>"><?php echo $cat_row['name'];?></a>
<?php

?>

this is the complete code.

Village Idiot 06-30-2008 09:37 PM

I am not fully understaing what is not working, but here are some things I have noticed that may be messing with things:
-Is your query returning with anything? Because your sprintf is looking for the string while you appear to be needing an integer.
-You check cat_result before your assign it, you also dont use a $ sign.

Your program will most likely die nothing (mysql_error will be blank) every time since you are checking a blank variable for being blank. It could also be of error that sprintf may be canceling the value out (this is, among other reasons, why I don't use sprintf).

codefreek 06-30-2008 09:46 PM

hum how can i still do the thing i am doing with the spaces,
with out sprintf?


EDIT:
while($cat_row = mysql_fetch_array($wes))
{
is causing problems :S

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 '= '2'' at line 1

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 90



Aaron: noting of that helped.
and PS, i did not want to have all the code on the same post to much better spreed it out,
PS,
Free Wordpress Themes
for wordpress styles.

Aaron 06-30-2008 11:31 PM

You need to get rid of the spaces around the equal sign... and your quotation marks seem weirdly placed.

Also, there is an edit button right next to quote post, so you don't need to keep replying :/

sketchMedia 06-30-2008 11:39 PM

From what i can gather, you want the news to be displayed depending on the cat GET request value, i.e. filtering if its specified?

Your code was abit of a mess, alot of breaking in and out of PHP cycles for no reason, i attempted to clean it up abit but it still needs work, but that said it now filters the news, i.e. index.php?cat=1 will display all news with cat_id of 1:

PHP Code:

<style type="text/css">
.style4 {
    font-size: xx-large;
    color: #FF9900;
}
</style>
<?php
error_reporting
(E_ALL & ~E_NOTICE);
include(
"db_connect.php");
session_start(); // Starts the session.

 
if ($_SESSION['logged'] != 1) { // There was no session found!

 
header("Location: users.php"); // Goes to login page.

 
exit(); // Stops the rest of the script.

 
}

echo 
'This is the main page!';
$res 'SELECT 
            `id`, `name`, `des`, `rest`, `tim` 
        FROM 
            `news`
        WHERE 
            `valid` = 1'
;

if(isset(
$_GET['cat']))
{
    
$res .= sprintf(" AND cat_id = '%d'"mysql_real_escape_string($_GET['cat']));
}
if(!
$web_result mysql_query($res)) 
{
    echo 
mysql_error();
}

while(
$website_row mysql_fetch_array($web_result)):
?>
<font size='3' color='CC0000'><strong>
<h1 class="style4">
    <?php print $website_row['des'];?> <br />
</h1>
</strong></font>


<table border="0">
    <tr>
        <th>NEWS:</th>
        <td><?php print $website_row['rest']; ?>
        </td>
    </tr>
</table>


<?php
endwhile;

echo 
'<a href="logout.php">Logout</a>';

$webq 'SELECT 
            `id`, `name`, `des` 
        FROM 
            `news` 
        WHERE 
            `valid` = 1'
;

if(isset(
$_GET['cat']))
{
    
$webq .= sprintf(" AND `cat_id` = '%d'"mysql_real_escape_string($_GET['cat']));
}
if(!
$web_result mysql_query($webq))
{
    echo 
mysql_error();
}
$catq 'SELECT 
            `id`, `name` 
        FROM 
            `cat`'
;
if(!
$cat_result mysql_query($catq))
{
    echo 
mysql_error();
}
while(
$cat_row mysql_fetch_array($cat_result)):
?>
<a href="index.php?cat=<?php echo $cat_row['id'];?>"><?php echo $cat_row['name'];?></a>
<?php
endwhile;

Sorry if this doesnt answer your problem, but its a bit vague.

codefreek 06-30-2008 11:42 PM

thank you but i keep it messy because its my way of learning i keep it dirty its my way but thank you for your help but i still want to figure out what the error was..
so i wont use your code :) but thank you so much for your time!

+ when i try to use the script the firefox stops
and prints cookie error :S

sketchMedia 06-30-2008 11:46 PM

Quote:

thank you but i keep it messy because its my way of learning i keep it dirty its my way but thank you for your help but i still want to figure out what the error was..
so i wont use your code :) but thank you so much for your time!
Dont worry m8, all the best programmers start off with messy code xD.

codefreek 06-30-2008 11:54 PM

sketchMedia:
have you any idea why i get cookie error?
when i test your script :S

codefreek 06-30-2008 11:55 PM

THIS IS THE USER CODE


PHP Code:

<?php
error_reporting
(E_ALL & ~E_NOTICE);
include(
"db_connect.php");
session_start(); // Starts the session.

if ($_SESSION[‘logged’] == 1) { // User is already logged in.

        
header("Location: index.php"); // Goes to main page.

        
exit(); // Stops the rest of the script.

} else {

if ( ! isset(
$username))
{
    
$username '';
}

if ( ! isset(
$password))
{
    
$password '';
}

$szForm = <<<FORM
<form action="users.php" name="login" method="post"> 
<table> 
    <tr><td>username</td>
        <td><input type="text" name="username" value="
{$username}" /></td>
    </tr> 

    <tr><td>password</td>
        <td><input type="password" name="password" value="
{$password}" /></td>
    </tr>

    <tr><td colspan="2"><input type='submit' name='login' value='login' /></td></tr> 
</table> 
</form>
FORM;

echo 
$szForm;  
   
    


               
$password mysql_real_escape_string($_POST['password']);
           
$username mysql_real_escape_string($_POST['username']);




               

                
$q mysql_query("SELECT * FROM users WHERE username = '$username'
                 AND password = '
$password'") or die (mysql_error()); // mySQL query

                
$r mysql_num_rows($q); // Checks to see if anything is in the db.

               

                
if ($r == 1) { // There is something in the db. The username/password match up.

                        
$_SESSION[‘logged’] = 1// Sets the session.

                        
header("Location: index.php"); // Goes to main page.

                        
exit(); // Stops the rest of the script.

                
} else { // Invalid username/password.

                        
exit("Incorrect username/password!"); // Stops the script with an error message.

                
}

        }



?>


sketchMedia 06-30-2008 11:58 PM

try moving the session_start(); to the top, as you are getting headers already sent errors.

delayedinsanity 07-01-2008 12:01 AM

Okay, first of all, and please don't take offense, but that's some messy code. Even starting out you should start to learn and employ practices that will greatly reduce and aid in your future debugging issues. You can learn some of these practices by paying attention to the coding practices of other members here (something that has greatly influenced and helped mine), or you can develop your own style ground up, but the more readable your code is, the better you are going to code, period.

Edit: I just noticed sketch told you this too, I'm not trying to rag on you for it, and trust me mine gets messy when I get going too. It's just good practice to clean up a little, especially when you start to have problems you can't figure out. Sometimes just cleaning things up will help you discover the problem.

Here's what I think you're trying to do, and with as little modification as possible (I left your tables in, as opposed to doing up some CSS for you, for example), how I might do it. See if it works for you.

PHP Code:

<?php

    
// Turn on error reporting and start the session
    
error_reporting(E_ALL);
    
session_start(); 

    include(
'db_connect.php');

    
// Look for our logged status, if not found redirect the user
    
if($_SESSION['logged'] != 1)
    {
        
header("Location: users.php");
        exit();
    }

    
define('DEBUG'1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style type="text/css">
h1 {
    font-size: 2em;
}

h2 {
    margin-bottom: 1em;
    font-size: 1.6em;
    color: #FF9900;
    font-weight: bold;
}

table, td {
    border: none;
    border-collapse: collapse;
}
</style>

<title>Codefreek's Page</title>
</head>

<body>

<h1>This is the main page!</h1>


<?php

    
if (isset($_GET['cat']))
    {

        
$category = (int) $_GET['cat'];

        
$q      sprintf("SELECT des, rest FROM `news` WHERE valid = 1 AND cat_id = %d"$category);
        
$result mysql_query($q);

        if( ! 
$result)
        {

            if (
defined('DEBUG'))
            {
                echo 
$q// so we can verify the query was properly formatted. Not really necessary here, but useful when you
                         // you're using dyanmically created query strings (such as those using $_GET variables)
                
echo mysql_error(); // Useful for debugging, but for a live site this will give useful information to a potential hacker, just so you know.
            
}
            else
            {
                echo 
"Category ID not found.";
            }

        }
        else
        {
    
            while(
$row mysql_fetch_assoc($result))
            {
                echo 
'<h2>'.$row['des'].'</h2>'
                echo 
'<table><tr><th>NEWS:</th><td>'.$row['rest'].'</td></tr></table>';
            }


         }

        
// Add some whitespace
        
echo "<br /><br />";

    }

    
// You had a second query on the 'news' table here, but it wasn't doing anything, so I just removed it?
    // In addition you were checking $cat_result in your if statement, when $cat_result had yet to be assigned.

    
$q "SELECT id, name FROM `cat`";
    
$result mysql_query($q);

    if( ! 
$result)
    {
        echo 
mysql_error();
    }
    else
    {

        while(
$row mysql_fetch_assoc($result))
        {

            echo 
'<a href="index.php?cat='.$row['id'].'">'.$row['name'].'</a><br />';

        }

    }

        echo 
'<br /><a href="logout.php">Logout</a>';

?>

</body>
</html>

-m

sketchMedia 07-01-2008 12:06 AM

Quote:

and trust me mine gets messy when I get going too
i agree, you should have seen some of mine today at work, truly embarrising lol until i cleaned it up

codefreek 07-01-2008 12:09 AM

if you are trying to give out pointers with a code please don't
give a snippet of code with a new error ;)

PS thank you!

PHP Code:

  if(!$result)
        {

            if (
defined('DEBUG'))
            { 

Quote:

ps, who said i wont fix it up later on?..the code..
think of how much time you save if you just code what comes to mind, and not worry about the damn css..

delayedinsanity 07-01-2008 12:11 AM

You guys had a nice little conversation there while I was going over everything. FINE. I'll just sit in the corner.
-m


All times are GMT. The time now is 02:13 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0