Questions...a few

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasonhardwick
Forum Newbie
Posts: 5
Joined: Thu May 22, 2008 1:03 pm

Questions...a few

Post by jasonhardwick »

ok i've got a set of questions that revolve around a network of users. Normally i would post them separate but just figured if anyone could help on anything that would be great... thanks

{ps: failure can get annoying}

let me explain a little.

I'm putting together a site that alows you to create a network of users. you can search for and view USER X's page and if you want invite him to your network you can via a php button in the right hand corner. this button adds USER X's username and a , to a coulmn in my user table called "network_invite". now that php button needs to change based on the "network_invite" & "network" columns in my user table. so if you art viewing USER X's page and he is already a "network member" (ie your name is in the "network" colum) your right hand corner button will then change to a "Remove from Network" button with a different action (php below).

and i know i should have built my database differently but i've got so many pages already built around it, i just want to get some of these pages working.

ok so my questions are:

1: When I run the current button php I always get an "Invite to network" button?
2: When i try to run the "Invite to network" script it replaces the value in my "network_invite" column insted of adding it to the end?
3: I have no clue on how to delete an username from a string in the "network" column?
4: I need help moving a "username," from a string in the "network_invite" column to a string in the "network" column?



Button Code:

Code: Select all

<?php
                
                include "config.php";
                    
                    $username=$_SESSION['username'];
                    $user_profile=$_GET['username'];
            
            
            
                $sql4 = "SELECT *
        from user
 WHERE uname = '$user_profile' 
        ";
        $sql5 = "SELECT *
        from user
 WHERE uname = '$username' 
        ";
 
$result4 = mysql_query($sql4) or die (mysql_error());
$row4 = mysql_fetch_assoc($result4);
 
$network_id4 = $row4['network'];
$network_id_parts4 = explode(",", $network_id4);
    $network_invite_id4 = $row4['network_invite'];
    $network_invite_id_parts4 = explode(",", $network_invite_id4);
    
    
 
$result5 = mysql_query($sql5) or die (mysql_error());
$row5 = mysql_fetch_assoc($result5);
 
$network_id5 = $row5['network'];
$network_id_parts5 = explode(",", $network_id5);
    $network_invite_id5 = $row5['network_invite'];
    $network_invite_id_parts5 = explode(",", $network_invite_id5);
 
 
 
//$user_profile=$_GET['username'];
 
 
if($network_invite_id_parts5 == $user_profile)
{
    echo "<form id=\"form1\" name=\"form1\" method=\"post\" action=\"accept_network.php\">";
    echo "<input name=\"network\" type=\"hidden\" id=\"network\" value ='$user_profile'/>";
    echo "<input name=\"invitee\" type=\"hidden\" id=\"invitee\" value ='$username'/>";
    echo "<label>"."<input type=\"submit\" name=\"button\" id=\"button\"  value=\"Accept in network\" />"."</label>";
    echo "</form>";
}
 
else {
 
 if($network_id_parts4 == $user_profile)
{
    echo "<form id=\"form2\" name=\"form2\" method=\"post\" action=\"remove_network.php\">";
    echo "<input name=\"network\" type=\"hidden\" id=\"network\" value ='$username'/>";
    echo "<input name=\"invitee\" type=\"hidden\" id=\"invitee\" value ='$user_profile'/>";
    echo "<label>"."<input type=\"submit\" name=\"button\" id=\"button\"  value=\"Remove from network\" />"."</label>";
    echo "</form>";
 
}
else 
{
 if($network_invite_id_parts4 == '$user_profile')
{
    echo "network invite pending";
 
}
else    
{
    echo "<form id=\"form3\" name=\"form3\" method=\"post\" action=\"add_invite.php\">";
    echo "<input name=\"network_invite\" type=\"hidden\" id=\"network_invite\" value = '$username'/>";
    echo "<input name=\"invitee\" type=\"hidden\" id=\"invitee\" value = '$user_profile'/>";
    echo "<label>"."<input type=\"submit\" name=\"button\" id=\"button\" value=\"invite to network\" />"."</label>";
    echo "</form>";
}}}
 
 
// swhile ($row = mysql_fetch_assoc($result)) {
                
            // }
                ?>

Accept To Network Code:

Code: Select all

<?php
 
include "config.php";
 
$tbl_name="user";
$user_profile=$_POST['invitee'];
$network=$_POST['network'];
 
$sql=("Select * from user where uname=''{$_SESSION['username']}''");
$result= mysql_query($sql); 
$row=mysql_fetch_assoc($result);
 
$exsisting = $row['network'];
 
$new_network = $exsisting.$network.",";
 
$sql1="UPDATE user SET network = '$new_network' WHERE uname = '$user_profile'";
 
$result1 = mysql_query($sql1) or die (mysql_error());
 
if($result1)
{
echo "Your have accepted ".$user_profile." to your network"."<BR/>";  
}
else
{
echo "Error";
}
 
?>
Remove from network code:

Code: Select all

<?php
 
include "config.php";
 
$tbl_name="user";
$user_profile=$_POST['invitee'];
$network=$_POST['network'];
 
$sql=("Select * from user where uname=''{$_SESSION['username']}''");
$result= mysql_query($sql); 
$row=mysql_fetch_assoc($result);
 
$exsisting = $row['network'];
 
$new_network = $exsisting.$network.",";
 
$sql1="UPDATE user SET network = '$new_network' WHERE uname = '$user_profile'";
 
$result1 = mysql_query($sql1) or die (mysql_error());
 
if($result1)
{
echo "Your Invite to".$user_profile."has been sent"."<BR/>";  
}
else
{
echo "Error";
}
 
?>
Invite to network code:

Code: Select all

<?php
 
include "config.php";
 
$tbl_name="user";
$user_profile=$_POST['invitee'];
$network_invite=$_POST['network_invite'];
 
$sql=("Select * from user where uname=''{$_SESSION['username']}''");
$result= mysql_query($sql); 
$row=mysql_fetch_assoc($result);
 
$exsisting = $row['network_invite'];
 
$new_network_invite = $exsisting.$network_invite.",";
 
$sql1="UPDATE user SET network_invite = '$new_network_invite' WHERE uname = '$user_profile'";
 
$result1 = mysql_query($sql1) or die (mysql_error());
 
if($result1)
{
echo "Your Invite to".$user_profile."has been sent"."<BR/>";  
}
else
{
echo "Error";
}
 
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Questions...a few

Post by RobertGonzalez »

Your button code needs to be written around the logic that IF you are on a page for a user that is in your network you show text that says "Remove" ELSE you show the "Add" text.

Appending data to a row in a query is like concatenating it in PHP (sort of):

Code: Select all

<?php $sql = "update `tableName` set `fieldName` = CONCAT(`fieldName`, '$user_to_add,') where `ID` = $userId"; ?>
Deleting a username is going to be a whole lot more difficult since you have a horribly denormalized table structure. I would suggest fixing this now since you are headed for a hell of a lot of heartache later on trying to fix this if things get really big.

Not sure I follow your last question.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Questions...a few

Post by califdon »

I strongly agree with Everah's comment. I know it SEEMS like a lot of work to fix your table structure, but it's a choice between fixing it now and having a smoothly running system, and MAYBE being able to fix your immediate problem, but getting into a TERRIBLE mess when the next change comes along.
Post Reply