sammy
sammy

Reputation: 717

make a favorite list of job using PHP and MYSQL

i have a database in my php page that included by:
name, family, phone_number, job, username and password
here is my code to select all job list:

<?php

$db_host = 'localhost';
$db_name= 'site';
$db_table= 'tablesite';
$db_user = 'root';
$db_pass = '';

$con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");
$selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
mysql_query("SET CHARACTER SET  utf8");
$dbresult=mysql_query("SELECT * FROM  $db_table WHERE job<>''",$con);
while($amch=mysql_fetch_assoc($dbresult))
{
echo "* name: "."&nbsp&nbsp&nbsp".$amch["name"]."   ".$amch["family"]."&nbsp&nbsp&nbsp"."* job: ".$amch["job"]."&nbsp&nbsp&nbsp"."* phone_number: ".$amch["phone_number"]."&nbsp&nbsp&nbsp"."* email: ".$amch["email"].'<br>';
}
?>

so i fetched all people that has a job, i need a favorite list, i mean when a user log in, can add some people to him/her favorite list, and the favorite list must be different than other user.

this is my login page:

<?PHP
require_once("./include/membersite_config.php");

if(isset($_POST['submitted']))
{
  if($fgmembersite->Login())
 {
    $fgmembersite->RedirectToURL("home2.php");
}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
      <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
      <title>Login</title>
      <link rel="STYLESHEET" type="text/css" href="style/fg_membersite.css"/>
      <script type='text/javascript' src='scripts/gen_validatorv31.js'>    </script>
</head>
<body>

<!-- Form Code Start -->
<div id='fg_membersite'>
<form id='login' action='<?php echo $fgmembersite->GetSelfScript(); ?>' method='post' accept-charset='UTF-8'>
<fieldset >
<legend>ورود</legend>

<input type='hidden' name='submitted' id='submitted' value='1'/>

<div class='short_explanation' dir="rtl">فیلدهای الزامی *</div>

<div><span class='error'><?php echo $fgmembersite->GetErrorMessage(); ?></span></div>
<div class='container' dir="rtl">
     <label for='username' >:نام کاربری *</label><br/>
    <input type='text' name='username' id='username' value='<?php echo $fgmembersite->SafeDisplay('username') ?>' maxlength="50" /><br/>
    <span id='login_username_errorloc' class='error'></span>
</div>
<div class='container' dir="rtl">
    <label for='password' >:کلمه عبور *</label><br/>
    <input type='password' name='password' id='password' maxlength="50" /><br/>
    <span id='login_password_errorloc' class='error'></span>
</div>

<div class='container'>
    <input type='submit' name='Submit' value='ورود' />
<!-- </div>
<div class='short_explanation'><a href='reset-pwd-req.php'>Forgot Password?</a></div>
</fieldset> -->
</form>
<!-- client-side Form Validations:
Uses the excellent form validation script from JavaScript-coder.com-->

<script type='text/javascript'>
// <![CDATA[

    var frmvalidator  = new Validator("login");
    frmvalidator.EnableOnPageErrorDisplay();
    frmvalidator.EnableMsgsTogether();

    frmvalidator.addValidation("username","req","Please provide your username");

    frmvalidator.addValidation("password","req","Please provide the password");

// ]]>
</script>
</div>
<!--
Form Code End (see html-form-guide.com for more info.)
-->

</body>

do i need a new db? or i can use of my current db with some more fields? use of foreign keys?

note: membersite_config.php is included by: db name, table name, host name, username and password.
it should act like a shopping cart, when a user want buy a thing first should login and then can add goods to him/her cart and also can remove items after adding them but here we need add users instead of goods
tahanks...

Upvotes: 0

Views: 1305

Answers (1)

Akshay
Akshay

Reputation: 2229

You definitely need a new table here. You can make a table in this way :-

Table name :- favouriters

indexid int(11) not null auto_increment,
username varchar(255) not null,
favourites varchar(255) not null

I've used indexid as an auto-increment column to make searching and deleting from the table much faster.

username is the common key between your users table and your Favourites table. You can connect both of them via this key.

Now here comes the major part, in this favourites column, you'll add the user's favourite members in a comma separated way. Like this.

favouriteuser1, favouriteuser2, favouriteuser3

Now, you can easily access all the favourite users of all the users. In this way, you can easily add a new username in the above list, or can remove a username from the above list.

You can use a simple LEFT JOIN to get the table data.

SELECT * FROM users LEFT JOIN favourites USING(username);

Upvotes: 0

Related Questions