Creating a dashboard feature for my website - php

I'm creating a dashboard feature for my website. I'm having trouble getting it sorted correctly. The dashboard will contain all of the recent status updates of the people who they are following. I'm using MySQL and PHP for this.
Status Table:
id: key value, auto-increments
user: the username of the poster of the status
status: the actual status that was posted
date: an int value, has the time that the status was posted
Users table: (Unneeded rows are excluded)
username: The user's name
following: All of the users that he is following. This is a text field, and is delimited by semicolons.
It needs to be sorted by the date posted. The reason I'm having trouble is because I have to get the people who the user is following. Any help?

Here i will have a go. I had to change sql tables slightly
Heres SQL:
CREATE TABLE IF NOT EXISTS `status` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`user_id` int(3) NOT NULL,
`user_status` text NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO `status` (`id`, `user_id`, `user_status`, `date`) VALUES
(1, 1, 'Hi, Im Dave, User One.', '2012-05-03'),
(2, 2, 'Hi, Im Amy, User Two.', '2012-05-01'),
(3, 3, 'Hi, Im Lisa user 3', '2012-05-01'),
(4, 4, 'Hi, Im Joe user 4', '2012-05-02');
CREATE TABLE IF NOT EXISTS `users` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`following` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO `users` (`id`, `username`, `following`) VALUES
(1, 'Dave', '2:3'),
(2, 'Amy', '1:4'),
(3, 'Lisa', '1:4'),
(4, 'Joe', '1:2:3');
and heres is the php (this presumes you have already connected to the database):
// Get user 2 (id 2) details from user table
$res = mysql_query("SELECT * FROM users WHERE id='2'");
while ($row = mysql_fetch_assoc($res)) {
$username = $row['username'];
$following = $row['following'];
}
if(!empty($following)) {
$data = explode(':',$following);
foreach($data as $user){
// Now get user 2 (id 2) followers statuses
$res = mysql_query("SELECT * FROM status WHERE user_id='$user'");
while ($row = mysql_fetch_assoc($res)) {
echo $user_status = $row['user_status'].'<br>';
echo $date = $row['date'].'<br>';
}
}
}
I tested and it seems to work just fine
hope it is what you need :)

Related

checkbox check with database data

i have two mysql tables and looks like as follows. iam using PHP and MySQL
CREATE TABLE IF NOT EXISTS `subject_category` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`sid` int(5) DEFAULT NULL,
`category` varchar(50) DEFAULT NULL,
`subject` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=191 ;
INSERT INTO `subject_category` (`id`, `sid`, `category`, `subject`) VALUES
(1, 1, 'GCE O/L', 'Sinhala'),
(2, 2, 'GCE O/L', 'Development Studies'),
(3, 3, 'GCE O/L', 'History'),
(4, 4, 'GCE O/L', 'Mathematics'),
(5, 5, 'GCE O/L', 'Citizan Education'),
and
CREATE TABLE IF NOT EXISTS `user_subjects` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`uid` bigint(20) NOT NULL,
`usid` int(4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=43 ;
INSERT INTO `user_subjects` (`id`, `uid`, `usid`) VALUES
(11, 142247454430186, 1),
(12, 142247454430186, 3),
(13, 142247454430186, 5)
actually what i need is, need to create check-box array using first table data. this part is already done. then i need to checked checkbox match with second table (subject_category.id=user_subjects.usid) for the particular user_subject.uid
FINALLY i need 5 check boxes for data in first table and 3 of them have selected with to second table values
please help me to solve this problem
I think you start off the wrong way, you should do it in 1 step, not in two steps.
The mysql query you need is:
$query = 'SELECT sc.subject, CASE WHEN us.id IS NULL THEN "" ELSE "checked" AS checked
FROM subject_category sc
LEFT JOIN user_subjects us ON (us.usid = sc.id)';
With the result of this query you should be able to generate the checked and unchecked checkboxes at once.
NOTE: I don't know which columns you need and if you needed sc.id or sc.sid in the join, but I think you can create the right query with this example.
Next step PHP (with mysql_* function as the TS asks in comment, I know you should use PDO or mysqli):
$result = mysql_query($query);
while ($line = mysql_fetch_assoc($result)){
echo '<input type="checkbox" checked="'.$line['checked'].'" name="subject" value="'. $line['subject'] .'" /> '.$line['subject'];
}

I want to display data according to product p_id

My 3 tables namely product, band, product Info
Product
CREATE TABLE IF NOT EXISTS `product` (
`p_id` int(11) NOT NULL AUTO_INCREMENT,
`product` varchar(50) NOT NULL,
PRIMARY KEY (`p_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
INSERT INTO `product` (`p_id`, `product`) VALUES
(1, 'Atta'),
(2, 'OIl');
Brand
CREATE TABLE IF NOT EXISTS `brand` (
`b_id` int(11) NOT NULL AUTO_INCREMENT,
`p_id` int(11) NOT NULL,
`brand` varchar(50) NOT NULL,
`image` varchar(255) NOT NULL,
PRIMARY KEY (`b_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
Insert
INSERT INTO `brand` (`b_id`, `p_id`, `brand`, `image`) VALUES
(1, 1, 'Ashirvad', 'FreeVector-Blue-Squares-Vector.jpg'),
(2, 1, 'Phillsberry', 'ILBAGNOALESSI_One_02.jpg'),
(3, 2, 'Sunflower', '001-bi-fold-corporate-brochure-template-vol-1-2.jpg');
Product Info
CREATE TABLE IF NOT EXISTS `product_info` (
`pi_id` int(11) NOT NULL AUTO_INCREMENT,
`pro` int(11) NOT NULL,
`b_id` int(11) NOT NULL,
`quantity` varchar(20) NOT NULL,
`measurement` varchar(20) NOT NULL,
`mrp` varchar(20) NOT NULL,
`our_price` varchar(20) NOT NULL,
PRIMARY KEY (`pi_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
Insert
INSERT INTO `product_info` (`pi_id`, `pro`, `b_id`, `quantity`, `measurement`, `mrp`, `our_price`) VALUES
(1, 1, 1, '1', 'kg', '50', '48'),
(2, 1, 2, '1', 'kg', '60', '59'),
(3, 2, 3, '1', 'ltr', '90', '86');
When i use the below query to display data according to the p_id, it displays data correctly
<?php
// Make a MySQL Connection
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not Connect to DB :'. mysql_error());
}
mysql_select_db("mr_bazaar",$con);
$result = mysql_query("select * FROM product");
echo '<ul class="list-1 p2">';
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo '<li>';
echo "<a href='page1.php?$row[p_id]'>";
echo '<b>';
echo $row['product'];
echo '</b>';
echo "</a>";
echo '</li>';
}
echo '</ul>';
mysql_close($con);
?>
and I want to display data according to the p_id in another page page1.php using the table product, brand, product_info(product from product table),(brand from brand table), (quantity, mrp from product_info table)
Send the product id to the next page as a URL parameter. So change,
echo "<a href='page1.php?$row[p_id]'>";
to
echo "<a href='page1.php?pid=$row[p_id]'>";
and accept the pid in page1.php using
$pid=$_GET['pid']; // getting the URL parameter
Now you can query to get the details corresponding to the pid.
mysql_query("SELECT p.product,b.brand,pi.quantity,pi.mrp FROM product as p INNER JOIN brand as b ON p.p_id=b.p_id
INNER JOIN product_info as pi ON pi.b_id=b.b_id WHERE p.p_id=$pid");
1- For sending the product id to page1.php, pass the variable correctly, change
echo "<a href='page1.php?$row[p_id]'>";
to
echo "<a href='page1.php?pid=$row[p_id]'>";
2- Get the variable on page1.php using:
$pid = $_GET['pid'];
3- Query the database for record of that product
$result = mysql_query("select * FROM product WHERE p_id='{$pid}'");
*Note:
1- I would recommend you not to use mysql_* function use mysqli or pdo library instead, and also escape the string while getting it from GET parameter.
2- You can use joins to fetch data from other tables corresponding to current product

Wrong Value showing in echo after request

I am trying to make a team name shown from a URL pulling information from my database regarding that team
<?
$query = "select * from teams where
name='".$mysqli->real_escape_string($_REQUEST['name'])."'";
$result = $mysqli->query( $query );
$row = $result->fetch_assoc();
$id = $row['id'];
$name = $row['name'];
$lon = $row['lon'];
$lat = $row['lat'];
$distance = $row['distance'];
$postcode = $row['postcode'];
$phone = $row['phone'];
?>
This worked fine until I put a second team name in the database and now all pages shows that name
the URL is http://domain.com/team.php?name=Test%20TeamA
and its showing Test TeamB and not the required one above
I have checked this on 2 pc's just to make sure its not something wrong with my form i used to put the data into my database or any values hanging about in my browser
why is this doing it?
SQL DUMP
--
-- Table structure for table `teams`
--
CREATE TABLE IF NOT EXISTS `teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`lat` varchar(32) NOT NULL,
`lon` varchar(32) NOT NULL,
`distance` varchar(20) NOT NULL,
`postcode` varchar(20) NOT NULL,
`phone` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
--
-- Dumping data for table `teams`
--
INSERT INTO `teams` (`id`, `name`, `lat`, `lon`, `distance`, `postcode`, `phone`) VALUES
(1, 'Test TeamA', '52.483038', '0.178962', '12.9', 'PE15 0JJ', ''),
(3, 'Test TeamB', '52.45645', '0.823423', '12', '', '01231223');
This is not the idea answer for a question like this but please see all the replies under my question
In my case it was a error on another page that i was including on my page
My best advice is to do what NickCoon had commented
echo $query;
to see the query that is being used. then strip all your page down to eliminate the issue

PHP MYSQL update table

I'm using MYSQL and php to insert data into my sql.
I have two tables users and userrights.
I'm using checkbox to display rights on user edit page. when rights are de-selected or select i wont to update my userrights table.
CREATE TABLE userrights (
userid int(11) NOT NULL default '0',
right_name varchar(50) collate latin1_general_ci NOT NULL default '',
PRIMARY KEY (userid,right_name)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
INSERT INTO userrights (userid, right_name) VALUES
(1, 'add'),
(1, 'delete'),
(1, 'edit'),
(1, 'view'),
(2, 'add'),
(2, 'delete'),
(2, 'edit'),
(2, 'view');
CREATE TABLE users (
id int(11) NOT NULL,
`name` varchar(100) collate latin1_general_ci default NULL,
email varchar(100) collate latin1_general_ci default NULL,
pass varchar(42) collate latin1_general_ci default NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
INSERT INTO users (id, name, email, pass) VALUES
(1, 'name', 'email#email.com', '5f4dcc3b5aa765d61d8327deb882cf99'),
(2, 'name name', 'email2#email.com', '1a1dc91c907325c69271ddf0c944bc72');
how do I do this?
I've had a similar problem before.
The solution I came up with is truncate the table and reinsert all of the selected entries.
You get the data of the checkbox(true or false)
$rows = mysql_num_rows(mysql_query("SELECT * FROM userrights WHERE id = 'x' and right_name = 'y'"); // Please use insted of mysql_* PDO or MySQLi
if ($rows > 0)
{
if ($checkbox == false)
{
mysql_query("DELETE FROM userrights WHERE id = 'x' AND right_name = 'y'");
}
}
else
{
// Recht nicht vorhanden --> if checkbox == true
if ($checkbox == true)
{
mysql_query("INSERT INTO... VALUES ($id, $right_name)
}
}
First you check if the user had the right. If yes you need to check if the right (data from checkbox) is set to false --> delete that row from the table.
If the user hadn't that right and the checkbox is true, you must insert that in your mysql_table.
The comments must be changed to and if-clause in your code.

Advanced many2many query for MYSQL

I im trying to build a imagegallery where people have access to different groups and the groups decide what catalogues and images they are allowed to see.
I though many2many structure would be best for this.
So far, ive manage to build the database like this:
image (image_name, image_file, image_id)
catalog (catalog_id, catalog_name)
supplier (supplier_id, supplier_name)
user (name, user_id)
image2cataloge (image_id, catalog_id)
image2supplier (image_id, supplier_id)
catalog2supplier (catalog_id, supplier_id)
user2supplier (user_id, supplier_id)
So... that been said, saving images and making supplier (or group if you want), adding users to supplier and linking images to supplier and catalogues is no problem. Inserting is no problem.
But selecting the right images based upon the users supplier setting and catalog they are in is harder.
For example, I have a user with user_id 1, which have access to supplier_id 1. supplier_id 1 have access to view catalogue 1, which holds images with image_id 1 and 2.
But supplier_id 1 only have access to image_id 2.
All this settings are stored in the database. How do I do the select query?
This is what i've tested;
//$catalog_id is the catalog_id we are in
//$user_id is the current users user_id
$sql = "SELECT i.*
FROM image i, user u, catalog cat, supplier s, supplier2user s2u, supplier2catalog s2c, image2catalog i2c, image2supplier i2s
WHERE u.id = '".$user_id."'
AND s2u.user_id = '".$user_id."'
AND s2u.supplier_id = s.id
AND s2c.catalog_id = '".$catalog_id."'
AND i2c.catalog_id = '".$catalog_id."'
AND i2s.supplier_id = s.id
AND s2c.supplier_id = s.id
GROUP BY i.id
ORDER BY i.name ASC
But when ive added more than one image, all images are shown for all users in all catalogues.
EDIT (2010/02/05):
Okey, so I've figured out how to at least show correct images in correct catalog. I do this by doing following:
$sql = "SELECT i.*
FROM
image i
INNER JOIN image2catalog i2c
ON i.id = i2c.image_id
AND i2c.catalog_id = '".$pages_level_0[$i]['id']."'
GROUP BY i.id
;";
This let's me output the correct images that belongs in the catalog the user is visiting at the moment. Now I just need to edit this query to filter out all images the user doesn't have access to. I very grateful for any help you can provide!
EDIT 2010/02/09:
---
CREATION SCHEME
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`email` varchar(250) NOT NULL,
`password` varchar(50) NOT NULL
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(250) character set utf8 NOT NULL default '',
`url` varchar(250) character set utf8 NOT NULL default '',
`childof` varchar(250) character set utf8 NOT NULL default '',
`hide` tinyint(4) NOT NULL,
`publishdate` varchar(14) NOT NULL,
`expiredate` varchar(14) NOT NULL,
`editdate` varchar(14) NOT NULL,
`editbygroup` varchar(250) NOT NULL,
`openby` varchar(250) NOT NULL,
`opendate` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
CREATE TABLE `imagebank_image` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(250) NOT NULL,
`img` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank_image2catalog` (
`image_id` int(11) NOT NULL,
`catalog_id` int(11) NOT NULL,
PRIMARY KEY (`image_id`,`catalog_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank_image2supplier` (
`image_id` int(11) NOT NULL,
`supplier_id` int(11) NOT NULL,
PRIMARY KEY (`image_id`,`supplier_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank_supplier` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank_supplier2catalog` (
`supplier_id` int(11) NOT NULL,
`catalog_id` int(11) NOT NULL,
PRIMARY KEY (`supplier_id`,`catalog_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank_supplier2user` (
`supplier_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`supplier_id`,`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
SOME DATA:
INSERT INTO `imagebank` (`id`, `name`, `url`, `childof`, `hide`, `publishdate`, `expiredate`, `editdate`, `editbygroup`, `openby`, `opendate`) VALUES
(1, 'Test 1', 'test-1', '', 0, '20100204230233', '', '', '', '', ''),
(2, 'Test 2', 'test-2', '', 0, '20100204230244', '', '', '', '', '');
INSERT INTO `imagebank_image` (`id`, `name`, `img`) VALUES
(1, 'Test img 1', 'labb_9noq80bik5.jpeg'),
(2, 'Test img 2', 'labb_53626114dz.jpeg');
INSERT INTO `imagebank_image2catalog` (`image_id`, `catalog_id`) VALUES
(1, 1),
(2, 2);
INSERT INTO `imagebank_image2supplier` (`image_id`, `supplier_id`) VALUES
(1, 2),
(2, 1);
INSERT INTO `imagebank_supplier` (`id`, `name`) VALUES
(1, 'Supplier1'),
(2, 'Supplier2'),
(3, 'Supplier3');
INSERT INTO `imagebank_supplier2catalog` (`supplier_id`, `catalog_id`) VALUES
(1, 2),
(2, 1);
INSERT INTO `imagebank_supplier2user` (`supplier_id`, `user_id`) VALUES
(1, 1),
(1, 11),
(1, 12),
(2, 1),
(2, 10),
(3, 1);
INSERT INTO `user` (`id`, `email`, `password`) VALUES
(1, 'User1#test.com', 'ff02dd5s33taa2ba5ff7c2c4d3327e444'),
(10, 'User2#test.com', 'ff02dd5s33taa2ba5ff7c2c4d3327e444'),
(11, 'User3#test.com', 'ff02dd5s33taa2ba5ff7c2c4d3327e444'),
(12, 'User4#test.com', 'ff02dd5s33taa2ba5ff7c2c4d3327e444');
WOW, now thats alot of stuff :P So I know that the tables, specially for "catalogs" which i call just "imagebank" might look abit strange. But I do have my reasons and thats not really the issue :) Its part of an even bigger picture. Hope this helps you to help me. Thanks again.
It looks like if you are passing in the user and catalogue id's then the supplier doesn't matter.
If you required the supplier information in the result, that would be a different matter.
It feels like you shouldn't be involving the user in this query at all as you seem to be looking for the images in a catalogue that are owned by a particular supplier.
If that is the case, then I would drop the requirement for the user id in the query and use the supplier id instead.
I am assuming that the user would have done the following to get to the point where they would be initiating this query:
login - obviously :)
click on 'list suppliers'
click on a supplier
click on a catalog
Either way you are going to have to do a lot of INNER JOIN's. For instance the query to retrieve the list of suppliers for a given user would be something like
SELECT
s.supplier_id,
s.Supplier_name
FROM
supplier s
INNER JOIN
user u
INNER JOIN
user2supplier u2s
ON
u.user_id = u2s.user_id
ON
u2s.supplier_id = s.supplier_id
WHERE
u.user_id = 3 -- for example...
(now, I haven't tested the SQL, but I think that is right...)
Let me know if I'm on the right track - if I have helped, I'd be happy to help some more if I can

Categories