Jump to content

[SOLVED] Few problems, explained in detail i think :)


Recommended Posts

Well here I am again with more questions that I need answering sigh well guess we'll start with what I think are the easiest even tho I canna get the thigs to work 8-)

 

Contents of post

1. Two errors need it to check to see if an alliance name is already taken plus the member id is always 127 even tho when the query is echoed the right mid is displayed

2. Combine two or more pages into one file

3. Display number in a number format → you need to look at the code

4. Count up since a member was last on

5. Question/help/advice – Best way to make an activation code/uniqid with a db check to see its not in use & a clickable activation code via email

 

1. Ive been working on an alliance page for an online game, now ive got quite far and then hit a brick wall it seems. What im trying to do in the code below is get it so it checks the db for alliances with the same name and then returns back and says "That alliance name is already taken" but no matter what I try alliances with the same name go through! Just thought I also need it to say “Your already in an alliance” and I guess “You may not see another alliance board” eek! Also for some strange reason the memberid (mid) is always “127” tho when I echo out the query it gives the right mid *puzzled whats up there?

<?php 
function getalliancename(){ 
$qc="SELECT `alliance` FROM `alliances` WHERE alliance='$alliance'"; 
echo "$qc<br>"; 
$r=@mysql_query($qc) or trigger_error("Query: $qc\n<br />MySQL Error: " . mysql_error); 
} 
$fdr = $user->userName;  // Gets username
$mid = $user->ID;  // Gets userID

  if ($cgi['submit']) { 
    if (!$cgi['name']){ 
    $echo= 'Please enter an alliance name'; 
    }elseif (getalliancename($cgi['name'])){ 
    $echo= "That alliance name is already taken"; 
    }else{ 
    $alliance = trim(addslashes($cgi['name'])); 

if ($alliance){ 
$q="INSERT INTO `alliances` (`alliance`, `mid`, `founder`, `members`) VALUES ('$alliance', '$mid', '$fdr', '1')"; 
echo "$q"; 
$r=@mysql_query($q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysql_error); 
}}} 
?>

 

2. How would I combine 2 pages into one? By that I mean im getting more and more pages everyday and some are very similar or ive had to make a new page to go somewhere else a few examples of this are: I have 2 edit and delete pages (one for news one for comments) The only difference  is the wording and the mysql querys, and another example is the alliances page right now I have alliances.php (where you choose your alliance name) then alliance.php where you view your members post on the alliance board etc etc. Is there a way I could combine these pages into one?

 

3. In the next section of code I need the $row['members'] to be displayed in number format like 1,000 instead of 1000 how would I archive this? The online book im using didnt explain this which is typical!

<?php 
//select an alliance 
$q="SELECT `alliance` FROM `alliances` WHERE ID='$id'"; 
echo "$q<br>"; 
$r=mysql_query($q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysql_error); 

$q="SELECT ID, alliance, members FROM `alliances` ORDER BY `members` DESC"; 
$r=@mysql_query($q); 
if ($r){ 
$bg='#000000'; 
while ($row = mysql_fetch_array($r, MYSQL_BOTH)){ 
$bg=($bg=='#000000' ? '#111111' : '#000000'); 
echo' 
                        <tr bgcolor="'.$bg.'"><td width="80%"><a href="alliance.php?id='.$row['ID'].'">'.$row['alliance'].'</td><td>'.$row['members'].'</td></tr>'; 
} 
echo'                
</tbody> 
</table>'; }?>

 

4. Just thought, right now im using the NOW() function to insert the time for recording ip loggins and then I display them in the following format “2008-07-17 10:48:34” is there a way I could have it count up from the last loggin? For example the date above would be 1day, x-hrs, x-seconds

<?php
$q = "SELECT `time`, `ip` FROM `IPs` WHERE `userID` = '$user->ID' ORDER BY `time` DESC";
                    $r = mysql_query($q) or die('Query failed: ' . mysql_error());
                    if ($r) {
                        while ($row = mysql_fetch_array($r, MYSQL_ASSOC)){
                            echo'
                            <tr>
                            <td align="left">'.$row['time'].'</td>
                            <td align="right">'.$row['ip'].'</td>
                            </tr>';
                        }
                    }
?>

 

One final thing what would be the best way to make an activation code/uniqid? One thats letters and numbers obviously I dont want 2 users to have the same uniqid so it would have to be checked in the db ok here is the real last thing how would I go about making a link where the user is sent an email telling them there username and password with a link that activates there account

 

I do have more problems but i'll leave them for another day :P

1. You have not passed $alliance on to your function. I am not sure if this is a class or not but you need to declare the variable $alliance for your first query. Either pass it to the function or set is globally.

<?php 
function getalliancename(){ 
global $alliance;
$qc="SELECT `alliance` FROM `alliances` WHERE alliance='$alliance'"; 
echo "$qc<br>"; 
$r=@mysql_query($qc) or trigger_error("Query: $qc\n<br />MySQL Error: " . mysql_error); 
} 
$fdr = $user->userName;  // Gets username
$mid = $user->ID;  // Gets userID

  if ($cgi['submit']) { 
    if (!$cgi['name']){ 
    $echo= 'Please enter an alliance name'; 
    }elseif (getalliancename($cgi['name'])){ 
    $echo= "That alliance name is already taken"; 
    }else{ 
    $alliance = trim(addslashes($cgi['name'])); 

if ($alliance){ 
$q="INSERT INTO `alliances` (`alliance`, `mid`, `founder`, `members`) VALUES ('$alliance', '$mid', '$fdr', '1')"; 
echo "$q"; 
$r=@mysql_query($q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysql_error); 
}}} 
?>

 

2.You can always use the same code on one page, then in the url, or post value you can set which table to use.

Example:

http://mydomain.com/alliance.php?page=alliances

Now you can use a switch to query the correct table and view the data

 

3.number_format() is your friend

'.number_format($row['members'], 0, '.', ',').'

 

4. Here is a function to display the difference in 2 dates. It is a little long but I made it to calculate ages for birthdays and such.

<?php
function datediff($date1, $date2){
  if($date1 > $date2){
  $month1 = date("m", $date1);
  $day1 = date("d", $date1);
  $year1 = date("Y", $date1);
  $hour1 = date("G", $date1);
  $min1 = date("i", $date1);
  $sec1 = date("s", $date1);
  $month2 = date("m", $date2);
  $day2 = date("d", $date2);
  $year2 = date("Y", $date2);
  $hour2 = date("G", $date2);
  $min2 = date("i", $date2);
  $sec2 = date("s", $date2);
  } else {
  $month1 = date("m", $date2);
  $day1 = date("d", $date2);
  $year1 = date("Y", $date2);
  $hour1 = date("G", $date2);
  $min1 = date("i", $date2);
  $sec1 = date("s", $date2);
  $month2 = date("m", $date1);
  $day2 = date("d", $date1);
  $year2 = date("Y", $date1);
  $hour2 = date("G", $date1);
  $min2 = date("i", $date1);
  $sec2 = date("s", $date1);
  }
    //seconds
    if($sec1 < $sec2){
    $secdiff = ($sec1+60)-$sec2;
    $min1--;;
    } else {
    $secdiff = $sec1-$sec2;
    }
    // minutes
    if($min1 < $min2){
    $mindiff = ($min1+60) - $min2;
    $hour1--;
    } else {
    $mindiff = $min1 - $min2;
    }
    // hours
    if($hour1 < $hour2){
    $hourdiff = ($hour1+24) - $hour2;
    $day1--;
    } else {
    $hourdiff = $hour1 - $hour2;
    }
    $amountdays = date("t", $month1);
    // day
    if($day1 < $day2){
    $daydiff = ($day1+$amountdays) - $day2;
    $month1--;
    } else {
    $daydiff = $day1 - $day2;
    }
    // months
    if($month1 < $month2){
    $monthdiff = ($month1+12) - $month2;
    $year1--;
    } else {
    $monthdiff = $month1 - $month2;
    }
    // years
    $yeardiff = $year1 - $year2;
    // Edit below to show just the values you want
    $datediff = $yeardiff."Yrs ".$monthdiff."Mon ".$daydiff."Days ".$hourdiff."Hrs ".$mindiff."Min ".$secdiff."Sec ";
return $datediff;
}

/*  Uncomment below to use your uorrent form */
$date1 = mktime('16', '20', '15', '12', '14', '2008');
$date2 = mktime('18', '30', '30', '1', '2', '2008');

echo datediff($date1, $date2)." Since last logon";
?>

 

5. Here is some code I use to mail an activation code to someone. you can add in the username and password to the email by just editing the $message. I made this with phpmailer.

<?php
if(isset($_POST['username']) && isset($_POST['password'])){
  foreach($_POST as $f => $v){
    if($f == "password"){
    $fd[] = $f;
    $va[] = md5($v);
    } else {
    $fd[] = $f;
    $va[] = $v;
    }
  }
$fd[] = "ip";
$va[] = $_SERVER['REMOTE_ADDR'];
$fd[] = "regdate";
$va[] = date("Y-m-d H:i:s");
$fd[] = "activation_key";
$code = md5(rand(100, 10000));
$va[] = $code;
$fields = implode("`, `", $fd);
$values = implode("', '", $va);
$sql = "INSERT INTO `users` (`$fields`) VALUES ('$values')"; // change this to match your table
$res = mysql_query($sql) or die(mysql_error());
  if($res){
  include('class.phpmailer.php'); // change this to point to the phpmailer class
  $mail = new PHPMailer();

  $mail->IsSMTP();                                // set mailer to use SMTP
  $mail->Host = "smtp.1and1.com";                 // specify main and backup server
  $mail->SMTPAuth = true;                         // turn on SMTP authentication
  $mail->Username = "xxxxxxx";  // SMTP username
  $mail->Password = "xxxxxxx";             // SMTP password
  //$mail->SMTPDebug = 2;
  $mail->SetLanguage('en', $_SERVER['DOCUMENT_ROOT'].'/phpmailer/language/');

  $message = "please click the link below to activate your account\n";
  $message .= "<a href=\"http://mydomain.com/index.php?do=activate&code=".$code."\" />http://mydomain.com/index.php?do=activate&code=".$code."</a>\n";

  $mail->From = "[email protected]";  // edit the from e-mail address
  $mail->FromName = "Support";  // Edit the name
  $mail->Subject = "Your account with Automarket.ro";  //edit the subject
  $mail->IsHTML(true);
  $mail->AddAddress($_POST['email']);
  $mail->Body = $message;
  $mail->AltBody = "To view the message, please use an HTML compatible email viewer!";

      if(!$mail->Send()){
    echo "Confirmation e-mail cannot be sent. Error: ".$mail->ErrorInfo;
    } else {
    echo "A confirmation e-mail has been sent to {$_POST['email']}";
    }
  } else {
  echo "You application cannot be completed";
  }
} else {
// show the form to register below

}
?>

 

Ray

 

 

 

ok i did as Ray said and added $alliance as a global unfortunatly it still does not check to see if the alliance name is taken and goes straight to insert :( anyone know whats up?

 

also the mid is always 127 even tho it should be 261 when i echo out the query insert it says 261 but then in the db its 127 i dunno whats up lol

<?php 
function getalliancename(){ 
global $alliance;
$qc="SELECT `alliance` FROM `alliances` WHERE alliance='$alliance'"; 
echo "$qc<br>"; 
$r=@mysql_query($qc) or trigger_error("Query: $qc\n<br />MySQL Error: " . mysql_error); 
} 
$fdr = $user->userName;  // Gets username
$mid = $user->ID;  // Gets userID

  if ($cgi['submit']) { 
    if (!$cgi['name']){ 
    $echo= 'Please enter an alliance name'; 
    }elseif (getalliancename($cgi['name'])){ 
    $echo= "That alliance name is already taken"; 
    }else{ 
    $alliance = trim(addslashes($cgi['name'])); 

if ($alliance){ 
$q="INSERT INTO `alliances` (`alliance`, `mid`, `founder`, `members`) VALUES ('$alliance', '$mid', '$fdr', '1')"; 
echo "$q"; 
$r=@mysql_query($q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysql_error); 
}}} 
?>

where is $user->ID being set??

 

Also, if you are querying the table to see if the alliance name exist, then you should put a check in to see if it is found.

 

also you need to return the alliance name not echo it.

<?php
function getalliancename(){
global $alliance;
$qc="SELECT `alliance` FROM `alliances` WHERE `alliance` = '$alliance'";
echo "$qc<br>";
$r=@mysql_query($qc) or trigger_error("Query: $qc\n<br />MySQL Error: " . mysql_error);
$found = mysql_num_rows($r);
  if($found > 0){
  $al = mysql_fetch_assoc($r);
  return $al['alliance'];
  } else {
  return FALSE;
  }
}
?>

 

your code is kind of confusing, you may want to post the whole class or page script to get some help on this.

 

Ray

Thanks again Ray for replying, i do put in a check to say if the name is in use

 

<?php
if ($cgi['submit']) { 
    if (!$cgi['name']){ 
    $echo= 'Please enter an alliance name'; 
    }elseif (getalliancename($cgi['name'])){ 
    $echo= "That alliance name is already taken"; 
    }else{ 
    $alliance = trim(addslashes($cgi['name'])); 
?>

that should echo out that the alliance name is in use/taken well thats what i thought :/

As for the $user->ID set in another file, ive used the exact same code on another file and it works no problem thats why im confussed about that! Basically all it does is get the userid of the person logged in so if i typed $user->userName and it would be displayed so if i typed <? echo $user-ID ?> it wold display the person who is logged in there ID number

I see what it is doing now. Since it is only a check no need to return the row, just a true of false will be fine

 

<?php
function getalliancename($alliance){
$qc="SELECT `alliance` FROM `alliances` WHERE `alliance` = '$alliance'";
$r=@mysql_query($qc) or trigger_error("Query: $qc\n<br />MySQL Error: " . mysql_error);
$found = mysql_num_rows($r);
  if($found > 0){
  return TRUE;
  } else {
  return FALSE;
  }
}
?>

 

Now this should work fine.

<?php
if ($cgi['submit']) { 
    if (!$cgi['name']){ 
    $echo= 'Please enter an alliance name'; 
    }elseif (getalliancename($cgi['name'])){ 
    $echo= "That alliance name is already taken"; 
    }else{ 
    $alliance = trim(addslashes($cgi['name'])); 
?>

 

Ray

 

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.