UGN Security
Ok, i have the following table:

Quote:
confessed.php?section=4
It'll take and list a random item of the "type" sction... I guess you'd probably need to see it to see what I'm tryin to do, so you can check out http://confess.undergroundnews.com/ I'm mainly making this script out of sheer boredom and trying to check how things work...
Damn it!!! mad


Gizmo PLEASE fix this [censored] no HTML tag crap in the code tags. I posted exactly what you wanted, but UBB would not post it besace I had > and <'s in the code.


Code
//php intro tage here
//the rand function in php see http://www.php.net/manual/en/function.rand.php
$random = rand(1, $highest_id_number_in_the_database);
$dg = mysql_query("Select * from ugn_confession WHERE id = '$random'");
While($dg2 = mysql_fetch_array($dg)){
echo "".$dg2['type'].""; 
/* to add more info just 
echo "any text you want to add ".$dg2['db_feild_name']."";
*/
}
Code
<html></html><?php ?>
works just fine for me... be sure you enclose the tags within [code][/code]
Code
<?php
//the rand function in php see http://www.php.net/manual/en/function.rand.php
 $random = rand(1, $highest_id_number_in_the_database);
 $dg = mysql_query("Select * from ugn_confession WHERE id = '$random'");
   While($dg2 = mysql_fetch_array($dg)){
   echo "".$dg2['type'].""; 
   /* to add more info just 
   echo "any text you want to add ".$dg2['db_feild_name']."";
   */
}
?>
Looks all fine and dandy, but I don't see anything in there to take and do section=4...

See, all the differant types are 1-8, I'd want section= to take and load a type, but a random sin of said type...
http://confess.undergroundnews.com/confessed.php?section=4

All it loads is a blank page... The EXACT code I'm using on confessed.php is (slightly differant than sintax):

Code
<?php require "connect.php"; ?>
<?php
$mysql_access = mysql_connect($host, $user, $pw) or die(mysql_error());
mysql_select_db($db) or die(mysql_error()); // make sure you add this ;)
?>

<?php
//the rand function in php see http://www.php.net/manual/en/function.rand.php
 $random = rand(1, $highest_id_number_in_the_database);
 $dg = mysql_query("Select *, DATE_FORMAT(date, '%H:%i on %d/%c/%Y') AS date2 from ugn_confession WHERE id = '$random'");
   While($dg2 = mysql_fetch_array($dg)){
   echo "".$dg2['type']."
";
   echo "".$dg2['date']."
";
   echo "".$dg2['id']."
";
   echo "".$dg2['name']."
";
   echo "".$dg2['confession']."
";
   /* to add more info just 
   echo "any text you want to add ".$dg2['db_feild_name']."";
   */
}
?>
See, what I'm goin for here is a little differant than what you think. See, there are 7 types of sin's, with 8 as miscelanious. Think of it as choosing a section. I want it so that people can basically do ?section=[number of the sin type] and it'll take and list a random sin that's in the database FOR that sin type...
well that is a horse of a different color.

Code
//**************************************************************
<?
//*  referinf URL  below                                       *            
//*  http://confess.undergroundnews.com/confess.php?section=#  *
//**************************************************************
//This code is assuming that type is your section
//number in the table at the top of this thread
//(post).

$dg = ("Select * FROM ugn_confession WHERE type = '$section'");
$num_rows = mysql_num_rows($dg);
$count = "1"; 

while($dg2 = mysql_fetch_array($dg)){
   if($count == "1"){
      $random1 = "".$dg2['id']."";
   }else{
   }
$count = $count++
   if ($count == "$num_rows"){
      $random2 = "".$dg2['id']."";
   }else{
   }
}
?>
Then just use my code above again... Hmmmm this would only work if all section info was stored in sequential order.... In other words

Code
 
Your_table
field1         feild2            type         id
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
bla            bla               1            1
bla            bla               1            2
bla            bla               2            3
bla            bla               2            4
bla            bla               2            5
bla            bla               3            6


//
//The above would work  but this would not
//

field1         feild2            type         id
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
bla            bla               1            1
bla            bla               3            2
bla            bla               2            3
bla            bla               5            4
bla            bla               6            5
bla            bla               1            6
 
My advise is store each section in a different table to allow you to index the data easier. Some thing like this in the code.


Code
 
if($section == "1"){
//query such and such table
}else if($section == "2"){
//query bla bla table
}else if($section == "3"){
//query this and that table
}
 
That would be easier than using one table.
mmmk
See post above
Very interesting... I may go your route, I wanted to do it with the smallest set of tables possible, but I guess that isn't too possible smirk... Ah well, it'll only take a few seconds to refine what I need...

One question, if I use them with each type having it's own query, what would the query be to echo the data to the page with only one row but have it randomize for each time it's posted...
Dude you can use the same code form the while function on. Like so..


Code
<?
//*********************Start code for section 1*********************
if($section == "1"){
   $random_number = rand(1,$highest_id_number_in_this_table);
      $dg = mysql_query(SELECT * FROM ugn_confession1 WHERE id = '$random_number');
//*********************Start code for section 3*********************
}else if($section == "3"){
   $random_number = rand(1,$highest_id_number_in_this_table);
       $dg = mysql_query(SELECT * FROM ugn_confession2 WHERE id = '$random_number');
}
//after all the if statements you just do one while statement
While($dg2 = mysql_fetch_array($dg)){
   echo "".$dg2['type']."
";   
   echo "".$dg2['date']."
";   
   echo "".$dg2['id']."
";   
   echo "".$dg2['name']."
";   
   echo "".$dg2['confession']."
";
}
See the only thing that is different is you have to handle the rand(); function and mysql_query in the if, else statements. The rest is the same because the variable $dg is passed to while just the same. The reason is only one $dg = mysql_query(); function will be processed. Make sense?

Also your table need to be constructed identicaly for this to work of course.


Now you could do it in one table IF.....

Section 1 is id numbers 1 - 500
Section 2 is id numbers 501 - 1000
Section 3 is id numbers 1001 - 1500

and so on


This would be difficult to update dynamicaly though. But if you did this this way the first script I posted today would work. But the id's have to be sequential to do the random functions I coded. Of course, maybe you could put it into an array... hmmmmmm

**note This may cause it to be slower for some reason. I have used arrays a bit lately and found they seem to slow things down a bit. But that is on my 2 servers
Solaris OS running apache and Redhat9 Running apache

Code
$dg = ("SELECT * FROM you_table WHERE id = '$section'");
$count = "0"
$number_rows = mysql_num_rows($dg);
WHILE($dg mysql_fetch_array($dg)){
$ugn_array = $dg2;
}

$random_num = (0, $number_rows);
echo "".$ugn_array['$random_num']['type']."
";
and so on...
that should work for you....
'eh I'll screw with it in the morning wink
Well it is now 17:43 local to me and Shhhh....


/me listens to crikets......

*churp churp churp*


Did it work, Which way did you go? Questions?
Well, I'm GOING to go with the seperate databases, but I'm tryin to think of the best route to go with the entering of info heh...
Damnit, now I'm confused about how to go about inputting the data! lol...

Ok, see input page above, how would I go about making my page input into the differant databases, I know I'd use if/else statements, but i'm not entirely sure how to go about it...

see: http://crap.undergroundnews.com/confess.zip for source.
Simple man...

You have a select box with the various section names like so

Code
<?
session_start();
if(session_is_registered(your_session_name_here)){
//
//
//
//The above is to secure the form.  So not just anyone can use it
//
//
//
//
?>
<form name=some_form id=some_form action="http://some_url.com">
   <select name=select_name>
     <option value=1>Section 1</option>
     <option value=2>Section 2</option>
     <option value=3>Section 3</option>
   </select>
<!--
The rest of the form elements for db entry go here
-->
<input type=submit value=submit>
<?
//
//closing out the session { below
//
}
//
//
//
?>
Then on the page the form data is sent to...

Code
<?
//
//Below is here so no on can submit using a URL hack
//
session_start();
if(session_is_registered(your_session_name_here)){

//
//
//Start checking for sections and processing data
//
//
  if($section_name == "1"){
  $dg = mysql_query(INSERT into Table_name (feild1, feild2, feild3,)VALUES ('$form_feild1', '$form_feild2', '$form_feild3'));
  if($dg){
?>
  <center>
   Insert sucessful, you will be re-directed now.
  <meta refresh=you_know_the_deal>
<?
  }else{
?>
  There is a issue at this time.  Please try back later.
<?
echo "error ".mysql_error();
//rember to remove this when the script is 100% ready
?>
  }

//
//
//Second section process
//
//
}else if($section == "2"){
  $dg = mysql_query(INSERT into Table_name2 (feild1, feild2, feild3,)VALUES ('$form_feild1', '$form_feild2', '$form_feild3'));
  if($dg){
?>
  <center>
   Insert sucessful, you will be re-directed now.
  <meta refresh=you_know_the_deal>
<?
  }else{
?>
  There is a issue at this time.  Please try back later.
<?
echo "error ".mysql_error();
//rember to remove this when the script is 100% ready
?>
  }
}
?>
Now to save yourself some time you could write a function. The coolest thing in php is the ability to write your own commands. Sooooo you can take this...
Code
  if($section_name == "1"){
  $dg = mysql_query(INSERT into Table_name (feild1, feild2, feild3,)VALUES ('$form_feild1', '$form_feild2', '$form_feild3'));
  if($dg){
?>
  <center>
   Insert sucessful, you will be re-directed now.
  <meta refresh=you_know_the_deal>
<?
  }else{
?>
  There is a issue at this time.  Please try back later.
<?
echo "error ".mysql_error();
//rember to remove this when the script is 100% ready
?>
  }

//
//
//Second section process
//
//
}else if($section == "2"){
  $dg = mysql_query(INSERT into Table_name2 (feild1, feild2, feild3,)VALUES ('$form_feild1', '$form_feild2', '$form_feild3'));
  if($dg){
?>
  <center>
   Insert sucessful, you will be re-directed now.
  <meta refresh=you_know_the_deal>
<?
  }else{
?>
  There is a issue at this time.  Please try back later.
<?
echo "error ".mysql_error();
//rember to remove this when the script is 100% ready
?>
  }
}
?>
and turn it into this


Code
insetion_cmd($table_name $section);
See this link for info.
http://us2.php.net/manual/en/functions.php#functions.user-defined


Functions can save you tons of time. On tradebikes for example I have one function that creates drop down and/or text boxes on the fly on all forms. It sets the CSS attributes and populates the drop downs all dynamicaly.


one drop down menu can be minimum 7 lines of code. I have it trimed down to one. and with a slight change it can be several other form elements. I trimed about 500+ lines off each bike entry page this way.
© UGN Security Forum