FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Simple php & mysql script

 


warallthetm
Code:
<?php
//We Are Including the bb code parser
include 'bbcode.php';

//Setting the Global Variables

$bbtext = $_POST['tut'];
$tutname = $_POST['title'];
$cat = $_POST['title'];
$username = $_COOKIE["usr"];
$ip = $_SERVER["REMOTE_ADDR"];
$date = date("l \\t\h\e jS");
//This tells the bb coder parser to process bbcode and turns to html
$htmltext = bb2html($bbtext);
// Now We Save to the Mysql Database

?>




How do I insert ($htmltext, $tutname, $username, $ip, $cat, and $date)
into a mysql database?


I also need a code so that i can list the articles info like this:



And when they click the link, it shows this:



I will Pay alot of frih for this
mathiaus
It would have been nice to have some db info & schema but I'll compromise and give you code which will need to be changed.


INSERT query
Code:
$query = "INSERT INTO `warallthe_tutorials`
('tutorial_content', 'tutorial_title', 'tutorial_user_id', 'ip', 'tutorial_cat_id', 'tutorial_date')
VALUES ('$htmltext', '$tutnam'e, '$username', '$ip', '$cat', '$date')";
if(!mysql_query($query)) exit('Problem iserting data into database');




really ROUGH idea for showing several tutorials
Code:
<table>
<?php

include('config.php');

$query = "SELECT t.tutorial_id, t.tutorial_name, t.tutorial_cat_id, t.tutorial_user_id,
u.user_id, u.username,
FROM `warallth_tutorials` t, `warallth_users` u
WHERE u.user_id = t.tutorial_user_id
LIMIT $start, $end
ORDER BY t.tutorial_id DESC";
if(!$result = mysql_query($query)) exit('Problem gathering data from database');

$i = 0;
while($row = mysql_fetch_array($result)) {
   if($i == 0) {
      echo '<tr>';
   }
   
   $catg = $row['tutorial_cat_id'];
   $image = $cat_image[$catg];
   $user_id = $row['user_id'];
   $username = $row['username'];
   $tutorial_id = $row['tutorial_id'];
   $tutorial_title = $row['tutorial_title'];
   
   echo '<td><img src="'.$image.'" alt="" />
      <hr />
      <a href="user.php?user_id='.$user_id.'">'.$username.'</a>
      <hr />
      <a href="view_tutorial.php?tutorial_id='.$tutorial_id.'">'.$tutorial_title.'</a>';
      
   
   $i++;
   
   if($i == 4) {
      echo '</tr>';
      $i = 0;
   }
}
?>
</table>





VERY ROUGH code for showing a tutorial
Code:
<?php

include('config.php');

$tut_id = (int)$_GET['tutorial_id'];

$query = "SELECT t.*, u.user_id, u.username
FROM `warallth_tutorials` t, `warallth_users` u
WHERE u.user_id = t.tutorial_user_id
AND t.tutorial_id='$tut_id'";
if(!$result = mysql_query($query)) exit('Problem gathering data from database');

$row = mysql_fetch_assoc($result);

echo '<h1><a href="view_tutorial.php?tutorial_id='.$row['tutorial_id'].'">'.$row['tutorial_title'].'</h1>
<br /><b>By <a href="user.php?user_id='.$row['user_id'].'">'.$row['username'].'</a></b>
<br /><br /><br />'.$row['tutorial_content'];

?>





Working from including a config file....
Code:
<?php

// CONFIGURATION
$db_host = 'localhost';
$db_user = 'warallth_tutorials';
$db_pass = '';
$db_db = 'warallth_tutorials';

// Category images
$cat_image[0] = 'html.gif';
$cat_image[1] = 'photoshop.gif';


// MySQL CONNECTION
$conn = mysql_connect($db_host, $sb_user, $db_pass);
mysql_select_db($db_db, $conn);


?>
warallthetm
I <3 you, let me look through the code and I will pay you what ever you want!!!
Also, if it might help, for my login system, I'm using this script:
http://www.evolt.org/article/PHP_Login_System_with_Admin_Features/17/60384/index.html
warallthetm
Kk, so when I have the sumbit article code i have this:
Quote:

<?php
//We Are Including the bb code parser
include 'bbcode.php';

//Setting the Global Variables

$bbtext = $_POST['tut'];
$tutname = $_POST['title'];
$cat = $_POST['cati'];
// this is just for testing, it will be cookie when were done
$username = $_POST['usr']; //$_COOKIE["usr"];
$ip = $_SERVER["REMOTE_ADDR"];
$date = date("l \\t\h\e jS");
//This tells the bb coder parser to process bbcode and turns to html
$htmltext = bb2html($bbtext);
//now we save to Mysql
// Include the Config file which connects to the mysql
include ('config.php');
// Inser this into the query
$query = "INSERT INTO `warallthe_tutorials`
('tutorial_content', 'tutorial_title', 'tutorial_user_id', 'ip', 'tutorial_cat_id', 'tutorial_date')
VALUES ('$htmltext', '$tutname', '$username', '$ip', '$cat', '$date')";
// If there is an error
if(!mysql_query($query)) exit("Problem Adding the Tutorial to the site Your tutorial article name: $tutname , and article: $htmltext was parsed for bb code though");
// If its all good, redirect to the success page
header( 'Location: added.html' ) ;
?>


It just outputs the exit message and dosnt save to mysql, and the page that shows the list of tuts dosnt work as well as the show tut page Sad
salman_500
Firstly ... add a field to your table... "tutorial_id" used for identifying each tutorial ... set it as primary and auto_increment ...

Code for entering data into table ..

Code:

// file containing code that connects to mysql..
include('mysqlconn.php');

//Insert into database...
$insertdata = mysql_query(INSERT INTO warallthe_tutorials (tutorial_content, tutorial_title, tutorial_user_id, ip, tutorial_cat_id, tutorial_date) VALUES ('$htmltext', '$tutname', '$username', '$ip', '$cat', '$date') or die(mysql_error());

// Check if data inserted ...
if ($insertdata) {
echo " Tutorial successfully added !";
}

// If not.. display error message....
else if (!$insertdata) {
echo " Error in adding the tutorial. Please try again or contact the administrator.";
}


Code for showing list and detail pages :

Code:


// Get tutorial id from URL
$tutid = $_GET['id'];

// If URL has defined tutorial id .. go to detail page...
if (isset($tutid)) {
include('includes/detail.php');
}

// If no id defined.... get list page ...
else if (!isset($tutid)) {
include('includes/list.php');
}


Code for list page :

Note: I have taken the name of you file as "tutorial.php" change it in the links if you have another name for it ...
Code:

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="25%"><b>Tutorial Name</b> </td>
    <td width="25%"><b>Category</b></td>
    <td width="25%"><b>Author</b></td>
    <td width="25%"><b>Added on</b> </td>
  </tr>
</table>

<?php
// file containing code that connects to mysql..
include('mysqlconn.php');

// Get tutorial data ....
$gettutorials = mysql_query("SELECT * FROM warallthe_tutorials ORDER BY tutorial_date DESC") or die (mysql_error());

// To show all data... shows all rows in simialr manner ...
while ($tutorial= mysql_fetch_array($gettutorials)) {

// HTML tables to show data in list ....
?>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><a href="tutorial.php?id=<?php echo $tutorial['tutorial_id']; ?>"><?php echo $tutorial['tutorial_title']; ?></a></td>
    <td><a href="tutorial.php?id=<?php echo $tutorial['tutorial_id']; ?>"><?php echo $tutorial['tutorial_cat_id']; ?></a></td>
    <td><a href="tutorial.php?id=<?php echo $tutorial['tutorial_id']; ?>"><?php echo $tutorial['tutorial_user_id']; ?></a></td>
    <td><a href="tutorial.php?id=<?php echo $tutorial['tutorial_id']; ?>"><?php echo $tutorial['tutorial_date']; ?></a></td>
  </tr>
</table>
<?php
}
mysql_free_result($gettutorial);
?>


ok now here is the code for the detail page :

Code:

<p>
  <?php
// file containing code that connects to mysql..
include('mysqlconn.php');

// Get tutorial data ....
$gettutorial = mysql_query("SELECT * FROM warallthe_tutorials WHERE id = '$tutid'") or die (mysql_error());

$tutorial= mysql_fetch_array($gettutorial);

// HTML tables to show data in list ....
?>
</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="30%"><b>Tutorial Name</b></td>
    <td width="70%"><?php echo $tutorial['tutorial_title']; ?></td>
  </tr>
  <tr>
    <td width="30%"><b>Category</b></td>
    <td width="70%"><?php echo $tutorial['tutorial_cat_id']; ?></td>
  </tr>
  <tr>
    <td width="30%"><b>Author</b></td>
    <td width="70%"><?php echo $tutorial['tutorial_user_id']; ?></td>
  </tr>
  <tr>
    <td width="30%"><b>Author IP</b> </td>
    <td width="70%"><?php echo $tutorial['ip']; ?></td>
  </tr>
  <tr>
    <td width="30%"><b>Added on</b></td>
    <td width="70%"><?php echo $tutorial['tutorial_date']; ?></td>
  </tr>
  <tr>
    <td width="30%"><b>Tutorial :</b> </td>
    <td width="70%">&nbsp;</td>
  </tr>
  <tr>
    <td height="20" colspan="2" align="center" valign="top"><table width="90%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td align="left" valign="top"><?php echo $tutorial['tutorial_content']; ?></td>
      </tr>
    </table></td>
  </tr>
</table>

  <?php
mysql_free_result($gettutorial);
?>


there... thats all you need.... ofcourse modify it to match you template.... if there are any error....plz dont mind... im outta touch... and if u cant fix an error... post here... ill do it ...

hope it helps !
warallthetm
Everything is working good, I made some minor mods, the only problem I'm having can be seen here:
http://www.thetutorialhost.net/tutorials.php
I only want the page to display 12 tutorials, then go to the next page with another 12 tutorials. How do I do this?
I'm using this code in list.php
Code:
<?php
// file containing code that connects to mysql..
include('mysqlconn.php');

// Get tutorial data ....
$gettutorials = mysql_query("SELECT * FROM warallth_tutorials ORDER BY tutorial_date DESC") or die (mysql_error());

// To show all data... shows all rows in simialr manner ...
while ($tutorial= mysql_fetch_array($gettutorials)) {

// HTML tables to show data in list ....
?>
<table width="200" class="table" align="left" cellpadding="0" cellspacing="0" >
  <tr>
    <td width="200"><a href="tutorials.php?id=<?php echo $tutorial['tutorial_id']; ?>"><img BORDER="0" src="/images/thumb/<?php echo $tutorial['tutorial_cat_id']; ?>.png" /></a></td>
  </tr>
  <tr>
    <td><a href="tutorials.php?id=<?php echo $tutorial['tutorial_id']; ?>"><?php echo $tutorial['tutorial_title']; ?></a></td>
  </tr>
  <tr>
    <td>By: <?php echo $tutorial['tutorial_user_id']; ?></td>
  </tr>
</table>
<?php
}
mysql_free_result($gettutorial);
?>
salman_500
ok here.... it should work... but if you face any errors... post full details here... ill have to refresh my memory on how this is supposed to work...lol Razz

Code:
<?php
// file containing code that connects to mysql..
include('mysqlconn.php');

// Get page number
$page = (int)$_GET['p'];

// Get record count ..
$records1 = mysql_query("SELECT COUNT(*) FROM warallth_tutorials") or die (mysql_error());
$records = mysql_result($records1, 0);

// Get some info about pages ....
$pages = floor(($records + 11) / 12);
$pages1 = ($pages - 1);

// Display this cool nav !!  :D
// Show arrows to move to next and prev pages ...
if(($page < '0') || ($page > $pages)) {
echo "You have reach a non-existing page number. Please use 'Back' on your browser and try again !<br>";
}
else {
echo " : ";
if ($page > '0') {
echo "<a href=\"tutorials.php?p=". ($page-1) ."\">&lt;</a>";
}
else {
echo "&lt;&lt; | &lt;";
}
for ($i = 1; $i <= $pages; ++$i) {
  echo " | <a href=\"tutorials.php?p=". ($i-1) ."\">". ($i) ."</a>";
}

if ($page < ($pages-1)) {
echo " | <a href=\"tutorials.php?p=". ($page+1) ."\">&gt;</a> | <a href=\"tutorials.php?p=". ($pages-1) ."\">&gt;&gt;</a>";
}
else {
echo " | &gt; | &gt;&gt;";
}

// Get tutorial data ....
$limit = (($page)*12);
$gettutorials = mysql_query("SELECT * FROM warallth_tutorials ORDER BY tutorial_date DESC LIMIT $limit,12") or die (mysql_error());

// To show all data... shows all rows in simialr manner ...
while ($tutorial= mysql_fetch_array($gettutorials)) {

// HTML tables to show data in list ....
?>
<table width="200" class="table" align="left" cellpadding="0" cellspacing="0" >
  <tr>
    <td width="200"><a href="tutorials.php?id=<?php echo $tutorial['tutorial_id']; ?>"><img BORDER="0" src="/images/thumb/<?php echo $tutorial['tutorial_cat_id']; ?>.png" /></a></td>
  </tr>
  <tr>
    <td><a href="tutorials.php?id=<?php echo $tutorial['tutorial_id']; ?>"><?php echo $tutorial['tutorial_title']; ?></a></td>
  </tr>
  <tr>
    <td>By: <?php echo $tutorial['tutorial_user_id']; ?></td>
  </tr>
</table>
<?php
}
mysql_free_result($gettutorials);
}
?>
Reply to topic    Frihost Forum Index -> Miscellaneous -> Marketplace

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.