Jump to content

Which Loop to use? Processing a forum


Lamez

Recommended Posts

Ok so my script ask you how many bowls you want to make (football bowls), then it stores it into the DB.

 

Well I want to make it where you can go back and edit the bowls, so I made a while loop to pull all the information, so my question is, how do I process the forum with out know how much data to process. I think I might need to make a foreach loop, but not sure.

Link to comment
Share on other sites

No I am sorry, let me explain better!

 

I am making a football pool, and the admin goes and types in how many bowls there will be, it can be any number, the script does not know. So I made it where the admin can go and edit the bowl information. So I made a while loop to pull the information from the database. Well I want to know how to process the information.

 

Here is entire code (kinda sloppy):

<?php
ob_start();
$path = "../../";
$title = "Manage Football Bowls";
$rank = "yes";
include ($path."main/include/cons/head.php");
?>
<SCRIPT TYPE="text/javascript">
function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key== || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}

//-->
</SCRIPT>
<?php

$salt = "KRAZYPICK-d-d-dS FOOTBALL :D adFADS :F:Asdf 4656+1131545641564548945645645";
$rand = rand(1, 10000);
$date = date('l jS \of F Y h:i:s A')+date(DATE_RFC822);

$get = md5($salt.$date.$user_);
$start_over = md5($get.$salt.$date.$user_).md5($user_);
$current = md5($get.$start_over.$user_.$date).md5($user_);
$num_con = md5($get.$current.$user_.$date.$start_over).md5($user_);
$add = md5($num_con.$current.$user_.$date.$start_over.$get.$salt).md5($user_);
$ask = md5($num_con.$current.$date.$start_over.$user_.$get.$salt.$add).md5($user_);
$dele = md5($user_.$ask.$add.$current.$num_con.$date.$get.$salt.$start_over).md5($user_);
$val = md5($user_.$ask.$dele.$add.$current.$num_con.$date.$get.$salt.$start_over).md5($user_);
$start_done = md5($get.$start_over.$current.$num_con.$add.$ask.$dele.$user_.$val.$salt).md5($user_);
$update = md5($get.$ask.$start_over.$current.$num_con.$dele.$val.$start_done.$date.$salt).md5($user_);

$idir = "bowls_img/";   // Path To Images Directory
$tdir = $idir;   // Path To Thumbnails Directory, --If the same as IMG DIR, it will only make thumbnails--Lamez
$twidth = "125";   // Maximum Width For Thumbnail Images
$theight = "100";   // Maximum Height For Thumbnail Images

if($session->logged_in){
  if($session->isAdmin()){  
  $result =  mysql_query("SELECT * FROM `foot_bowls`"); 
  echo '<p class="header">Manage Football Bowls</p><p class="maintext">';
  
  if (!isset($_GET[$get])) {
  

echo '<DD>';
unset($_SESSION['asked']);
   if (mysql_num_rows($result) !== 0) {
   echo '<li><a href="?'.$get.'='.$current.'">Edit Current Bowl Setup</a><br />';
   }
  echo '<li><a href="?'.$get.'='.$start_over.'">New Bowl Setup</a>';
  
  }else if (isset($_GET[$get]) && $_GET[$get] == $start_over) {
     if (mysql_num_rows($result) !== 0) {
   header("Location: ?".$get."=".$ask);
   }
?>
<form id="form1" name="form1" method="post" action="?<?php echo $get; ?>=<?php echo $num_con ?>">
  Number of Bowls: 
  <label>
  <input name="bowl_num" type="text" id="bowl_num" size="5" maxlength="5" onkeypress="return numbersonly(this, event)" />
  </label>
  <label>
  <input type="submit" name="con_num" id="con_num" value="Continue" />
  </label>
</form>
<?php
  }else if (isset($_GET[$get]) && $_GET[$get] == $update) {


  echo $_POST['id'];
  
  }else if (isset($_GET[$get]) && $_GET[$get] == $current) {
  
echo '<form method="post" action="?'.$get.'='.$update.'" enctype="multipart/form-data">
<table width="100%" border="0">
  <tr>
    <td width="6%" align="center" valign="top"><strong>ID</strong></td>
    <td width="13%" align="center" valign="top"><strong>Name</strong></td>
    <td width="13%" align="center" valign="top"><strong>Date</strong></td>
    <td width="13%" align="center" valign="top"><strong>Time</strong></td>
    <td width="13%" align="center" valign="top"><strong>Network</strong></td>
    <td width="12%" align="center" valign="top"><strong>Place</strong></td>
    <td width="30%" align="center" valign="top"><strong>Image</strong></td>
  </tr>';

$q = mysql_query("SELECT * FROM `foot_bowls` ORDER BY `id` ASC");
while($row = mysql_fetch_array($q)){
$id = $row['id'];
$name = $row['name'];
$date = $row['date'];
$time = $row['time'];
$network = $row['network'];
$place = $row['place'];
$img = $row['image'];
?>
  <tr>
    <td align="center" valign="top"><?php echo $id; ?><input name="id" type="hidden" id="id" value="<?php echo $id; ?>"></td>
    <td align="center" valign="top"><label>
      <input name="name"  value="<?php echo $name; ?>" type="text" id="<?php echo $name; ?>" maxlength="100" />
    </label></td>
    <td align="center" valign="top"><label>
      <input name="date" value="<?php echo $date; ?>" type="text" id="<?php echo $date; ?>" maxlength="500" />
    </label></td>
    <td align="center" valign="top"><label>
      <input name="time" value="<?php echo $time; ?>" type="text" id="<?php echo $time; ?>" maxlength="100" />
    </label></td>
    <td align="center" valign="top"><label>
      <input name="network" value="<?php echo $network; ?>" type="text" id="<?php echo $network; ?>" maxlength="100" />
    </label></td>
    <td align="center" valign="top"><label>
      <input name="place" value="<?php echo $place; ?>" type="text" id="<?php echo $place; ?>" maxlength="100" />
    </label></td>
    <td align="center" valign="top"><center> <a href="?"><img src="<?php echo $path.$img; ?>" /><br /> <a href="?">Change Above Image</a></center></td>
  </tr>

<?php
} //end while loop
  echo '<tr>
    <td colspan="7"><label>
      <input type="submit" name="button" id="button" value="Submit" />
    </label></td>
    </tr>
</table>
<label></label>
</form>';
  
  
  }else if (isset($_GET[$get]) && $_GET[$get] == $num_con) {



    if (!isset($_POST['con_num'])){
     header("Location: ?".$get."=".$start_over);
    }


$num = $_POST['bowl_num'];
if ($num == (0)){
header("Location: ?".$get."=".$start_over);
}

echo '<form method="post" action="?'.$get.'='.$add.'" enctype="multipart/form-data">
<table width="100%" border="0">
  <tr>
    <td width="6%"><strong>ID</strong></td>
    <td width="13%"><strong>Name</strong></td>
    <td width="13%"><strong>Date</strong></td>
    <td width="13%"><strong>Time</strong></td>
    <td width="13%"><strong>Network</strong></td>
    <td width="12%"><strong>Place</strong></td>
    <td width="30%"><strong>Image (.jpeg and .jpg only!)</strong></td>
  </tr>';

  for ($counter = 1; $counter <= $num; $counter++) {
?>
  <tr>
    <td><?php echo $counter; ?></td>
    <td><label>
      <input name="name-<?php echo $counter; ?>" type="text" id="name-<?php echo $counter; ?>" maxlength="100" />
    </label></td>
    <td><label>
      <input name="date-<?php echo $counter; ?>" type="text" id="date-<?php echo $counter; ?>" maxlength="500" />
    </label></td>
    <td><label>
      <input name="time-<?php echo $counter; ?>" type="text" id="time-<?php echo $counter; ?>" maxlength="100" />
    </label></td>
    <td><label>
      <input name="network-<?php echo $counter; ?>" type="text" id="network-<?php echo $counter; ?>" maxlength="100" />
    </label></td>
    <td><label>
      <input name="place-<?php echo $counter; ?>" type="text" id="place-<?php echo $counter; ?>" maxlength="100" />
    </label></td>
    <td><input type="file" name="image-<?php echo $counter; ?>" class="form" id="image-<?php echo $counter; ?>"></td>
  
  
<?php  
   }//end for loop
echo '
</tr>
  <tr>
    <td colspan="7"><label>
      <input type="submit" name="button" id="button" value="Submit" />
    </label></td>
    </tr>
</table>
<label></label>
</form>';


$_SESSION['num'] = $num;
$_SESSION['up'] = true;

   //code here!
  }else if (isset($_GET[$get]) && $_GET[$get] == $start_done) {
    echo "Added ".$num." bowls";
  echo '<DD>';
  echo '<li><a href="?">Manage Football Bowl Home</a>';
  echo '<li><a href="../index.php">Admin Center</a>'; 
   
  }else if (isset($_GET[$get]) && $_GET[$get] == $add) {
  
  $num = $_SESSION['num'];
    if($_SESSION['up'] !== (true)){
 header("Location: ?".$get."=".$start_over);
}

function getExt($str) {

        $i = strrpos($str,".");
        if (!$i) { return ""; }

        $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);

        return $ext;

}
   for ($counter = 1; $counter <= $num; $counter++) { 
   //Image Settings!

   
     $url = $_FILES['image-'.$counter]['name'];   // Set $url To Equal The Filename For Later Use
  $ext = getExt($url);
  
  if ($_FILES['image-'.$counter]['type'] == "image/jpg" || $_FILES['image-'.$counter]['type'] == "image/jpeg" || $_FILES['image-'.$counter]['type'] == "image/pjpeg") {
    $file_ext = strrchr($_FILES['image-'.$counter]['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
$name = $counter.".".$ext;
$url = $name;
    $copy = copy($_FILES['image-'.$counter]['tmp_name'], "$idir" . $name);   // Move Image From Temporary Location To Permanent Location
    if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location
     // print 'Image uploaded successfully.<br />';   // Was Able To Successfully Upload Image
      $simg = imagecreatefromjpeg("$idir" . $url);   // Make A New Temporary Image To Create The Thumbanil From
      $currwidth = imagesx($simg);   // Current Image Width
      $currheight = imagesy($simg);   // Current Image Height
      if ($currheight > $currwidth) {   // If Height Is Greater Than Width
         $zoom = $twidth / $currheight;   // Length Ratio For Width
         $newheight = $theight;   // Height Is Equal To Max Height
         $newwidth = $currwidth * $zoom;   // Creates The New Width
      } else {    // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
        $zoom = $twidth / $currwidth;   // Length Ratio For Height
        $newwidth = $twidth;   // Width Is Equal To Max Width
        $newheight = $currheight * $zoom;   // Creates The New Height
      }
      $dimg = imagecreate($newwidth, $newheight);   // Make New Image For Thumbnail
      imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete
      $palsize = ImageColorsTotal($simg);
      for ($i = 0; $i < $palsize; $i++) {   // Counting Colors In The Image
       $colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used
       ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use
      }
      imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It)
      imagejpeg($dimg, "$tdir" . $url);   // Saving The Image
      imagedestroy($simg);   // Destroying The Temporary Image
      imagedestroy($dimg);   // Destroying The Other Temporary Image
}
  //Add to the database++   
}
$name = "admin_area/foot/bowls_img/".$name;
  $image = $name;
  $id = $counter;
  $name = $_POST['name-'.$counter];
  $date = $_POST['date-'.$counter];
  $time = $_POST['time-'.$counter];
  $network = $_POST['network-'.$counter];
  $place = $_POST['place-'.$counter];
  
  $query = "INSERT INTO `foot_bowls` (id, name, image, date, time, network, place) VALUES ('".$id."', '".$name."', '".$image."', '".$date."', '".$time."', '".$network."', '".$place."')";
  mysql_query($query)or die(mysql_error());
  }//end loop
  
  header("Location: ?".$get."=".$start_done);

  
  //Delete Area--++
  }else if (isset($_GET[$get]) && $_GET[$get] == $ask) {
  if (mysql_num_rows($result) == 0) {
   header("Location: ?".$get."=".$start_over);
  }
  $_SESSION['ask'] = true;
  echo "In order to continue, you need to delete the old bowl settings.<br /><br />";
?>
<form id="form1" name="form1" method="post" action="?<?php echo $get; ?>=<?php echo $val; ?>">
  <label>
  <input name="ask" type="radio" id="radio" value="no" checked="checked" />
  </label>
  Nevermind! 
  <label>
  <input type="radio" name="ask" id="radio2" value="yes" />
  </label>
  Delete and Continue 
  <label>
  <input type="submit" name="dele_ask" id="dele_ask" value="Continue" />
  </label>
</form>
<?php
  }else if (isset($_GET[$get]) && $_GET[$get] == $val) {
    if (mysql_num_rows($result) == 0) {
   header("Location: ?".$get."=".$start_over);
  }
  if (!isset($_SESSION['ask'])){
  header("Location: ?".$get."=".$ask);
  }
  unset($_SESSION['ask']);
  
  $_SESSION['asked'] = true;
  if($_POST['ask'] == ("no")){
  header("Location: ?");
  }
  if($_POST['ask'] == ("yes")){
  header("Location: ?".$get."=".$dele);
  }else{
  header("Location: ?");
  }
  
  }else if (isset($_GET[$get]) && $_GET[$get] == $dele) {
    if (mysql_num_rows($result) == 0) {
   header("Location: ?".$get."=".$start_over);
  }
  if (!isset($_SESSION['asked'])){
   header("Location: ?".$get."=".$ask);
  }

mysql_query("DELETE FROM `foot_bowls` WHERE `id` > '0'");
foreach(glob($idir.'*.jpg') as $file){
     unlink($file);
}
foreach(glob($idir.'*.jpeg') as $file){
     unlink($file);
}
foreach(glob($idir.'*.pjpeg') as $file){
     unlink($file);
}
echo "Deleted old Bowls";

   header("Location: ?".$get."=".$start_over);

  
  unset($_SESSION['ask']);
  unset($_SESSION['asked']);
  }
  }else{
  header('Location: '.$path.'index.php');
  }
}else{
header('Location: '.$path.'index.php');
}
include ($path."main/include/cons/foot.php");
?>

 

Hoped I explained it better.

 

Link to comment
Share on other sites

WAY too much code to go through, but the principle behind letting the user edit all the bowls at once is simple.  add a checkbox next to each row / bowl, with an array name and a value of that bowl's ID:

 

<input type="checkbox" name="edit[]" value="BOWL_ID" />

 

obviously, you'll want to echo the bowl's ID into that value attribute.  the other information should be tied to the bowl ID, for example the title input would look like this:

 

<input type="text" name="bowl_title[bOWL_ID]" value="echo the current title here" />

 

when you hit the backend, you can foreach() through the checkboxes and use that ID to construct an UPDATE query for that bowl ID:

 

foreach ($_POST['edit'] AS $bowl_id)
{
  $query = "UPDATE table SET title='{$_POST['bowl_title'][$bowl_id]}' WHERE id='$bowl_id'";
}

 

keep in mind i haven't put ANY error checking or data validation.  that's something you'll need to work out.  hope this explains the process for you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.