Jump to content

[SOLVED] Help with a script..


Cory94bailly

Recommended Posts

Test1.php:

 

 

<title>Recent News</title>
<a href="test2.php">Click Here For The <i>Admin End</i></a>

<h1>Recent News</h1>

<?php
mysql_connect("***", "***", "***") or die(mysql_error());
mysql_select_db("***") or die(mysql_error());

$query1 = "SELECT * FROM sticky"; 
$query2 = "SELECT * FROM news";

$result1 = mysql_query($query1) or die(mysql_error());
$result2 = mysql_query($query2) or die(mysql_error());


while($row1 = mysql_fetch_array($result1))
while($row2 = mysql_fetch_array($result2)){
        echo "<table>";
        echo "<tr>";
        echo "<td>";
        echo "<h2>";
        echo "<u>";
        echo $row1['title'];
        echo "</u>";
        echo "</h2>";
        echo "</tr>";
        echo "</td>";
        echo "<tr>";
        echo "<td>";
      echo $row1['news'];
      echo "</td>";
      echo "</tr>";
        echo "<br />";
        echo "<br>";
        echo "<table>";
        echo "<tr>";
        echo "<td>";
        echo "<h2>";
        echo "<u>";
        echo $row2['title'];
        echo "</u>";
        echo "</h2>";
        echo "</tr>";
        echo "</td>";
        echo "<tr>";
        echo "<td>";
      echo $row2['news'];
      echo "</td>";
      echo "</tr>";
        echo "<br />";
}
?>

 

 

 

Test2.php:

 

<?php

   // Make a MySQL Connection
    mysql_connect("***", "***", "***") or die(mysql_error());
    mysql_select_db("***") or die(mysql_error()); 

if(isset($_POST['submit'])) { 
   
   $title = mysql_real_escape_string($_POST['title']);
   $news = mysql_real_escape_string($_POST['news']);
   
   // Insert the news
   $query = mysql_query("INSERT INTO news (title, news) VALUES ('$title', '$news')")or die(mysql_error());   
   
   echo "News posted!<br><a href='test1.php'>Click here to view the news!</a>";

}
elseif(isset($_POST['delete']))
{
   // Delete it all!
   mysql_query("TRUNCATE TABLE news")or die(mysql_error());
   
   echo "All news deleted!<br><a href='test1.php'>Click here to check!</a>";
      
}
elseif(isset($_POST['sticky']))
{
   $title = mysql_real_escape_string($_POST['title']);
   $news = mysql_real_escape_string($_POST['news']);
   
   // Insert the news
   $query = mysql_query("INSERT INTO sticky (title, news) VALUES ('$title', '$news')")or die(mysql_error());   
   
   echo "<b>Sticky</b> news posted!<br><a href='test1.php'>Click here to view the news!</a>";
      
}
else 
{
?>

<title>Add/Delete Recent News</title>
<a href="test1.php">Click Here For The <i>User End</i></a>

<h2>This is the <i>admin end</i> of my php news script.. Still working on it though xD </h2>
   <form action="<?php echo $_SERVER['PHP_SELF'], '?postnews=1' ?>" method="post">
      Title:<br>
      <input type="text" name="title"></input><br><br>
      Text:<br>
      <textarea rows="10" cols="50" name="news"></textarea>
      <br><br>
      <input type="submit" name="submit" value="Submit"> 
      <form action="<?php echo $_SERVER['PHP_SELF'], '?postnews=2' ?>" method="post">
     <input type="submit" name="sticky" value="Post this Sticky">
   </form>  
   </form>

<br><br>

<h4>Options:</h4>

<form action="<?php echo $_SERVER['PHP_SELF'], '?deleteall=1' ?>" method="post">
<input type="submit" name="delete" value="Delete all news">
</form> 

<u>Current features:</u>
<br>
<i>Quick Post News
<br>
Delete all news with one click
<br>
Option to title your news</i>
<br><br><br>
<u>Coming soon:</u>
<br>
<i>"Sticky" News
<br>
Edit news</i>

<?php
}
?>

 

 

 

 

 

 

Ok there's the two pages..

 

Now my problem is that when I post "stickied" news, it doesn't get shown on test1.php...

 

Am I doing something wrong?

Link to comment
https://forums.phpfreaks.com/topic/105350-solved-help-with-a-script/
Share on other sites

Your while loops look odd. I would do it this way if I were you:

 

<?php
mysql_connect("***", "***", "***") or die(mysql_error());
mysql_select_db("***") or die(mysql_error());

$query1 = "SELECT * FROM sticky"; 
$query2 = "SELECT * FROM news";

$result1 = mysql_query($query1) or die(mysql_error());
$result2 = mysql_query($query2) or die(mysql_error());

while($row1 = mysql_fetch_array($result1))
printNews($row1);

while($row2 = mysql_fetch_array($result2))
printNews($row2);

function printNews($news)
{
echo '
<table><tr>
<td><h2><u>' . $news['title'] . '</u></h2></td>
</tr><tr>
<td>' . $news['news'] . '</td>
</tr></table><br />';

}

 

This will print all stickies, then all news (if that's what you're actually trying to do)

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.