Jump to content

Need help with PHP Join sql willing to pay through Paypal!


xtrax

Recommended Posts

I hope I am posting this in the right forum and please accept my appolgieze if I made an error.

 

I have two MySQL Tables I am tring to get information from the one which uses the Category Name where the other one only uses the category ID. What I am tring to do is list what categories a user has submitted the articles to as its associated with the USERS AuthorID..

 

The first table is called tarticles and this one stores the category ID and the AuthorID that submitted it.

 

The second table is called tcategories and this stores the Name of the category as well as the ParentID of the category...

 

What I am tring to do is associate the AuthorID who submitted to whatever category with the category name if that makes any sense...

 

I am probably making this more confusing then what it needs to be but I hope someone gets what I am referring too..

 

Here is the structure of table 1

 

table tarticles structure

CREATE TABLE IF NOT EXISTS `tarticles` (
  `intId` int(11) NOT NULL auto_increment,
  `intAuthorId` int(11) NOT NULL default '0',
  `intCategory` int(11) NOT NULL default '0',
  `varArticleTitle` varchar(255) NOT NULL default '',
  `textSummary` mediumtext NOT NULL,
  `varKeywords` varchar(255) NOT NULL default '',
  `textResource` mediumtext NOT NULL,
  `intStatus` tinyint(1) NOT NULL default '0',
  `ttSubmitDate` date NOT NULL,
  `word_count` mediumint(9) NOT NULL default '0',
  `SubmittedBy` varchar(50) NOT NULL default 'live',
  `declined` int(1) NOT NULL default '0',
  `hits` varchar(12) NOT NULL,
  `views` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`intId`),
  UNIQUE KEY `varArticleTitle` (`varArticleTitle`),
  KEY `intCategory` (`intCategory`),
  KEY `intAuthorId` (`intAuthorId`),
  KEY `varKeywords` (`varKeywords`),
  KEY `ttSubmitDate` (`ttSubmitDate`),
  KEY `word_count` (`word_count`),
  KEY `SubmittedBy` (`SubmittedBy`),
  KEY `intStatus` (`intStatus`),
  KEY `declined` (`declined`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1083 ;

 

Second table structure tcategory and the row called varCategory stores the Name of the Category

CREATE TABLE IF NOT EXISTS `tcategories` (
  `intID` int(11) NOT NULL auto_increment,
  `varCategory` varchar(200) NOT NULL default '',
  `textDescription` mediumtext NOT NULL,
  `intHasChild` int(11) NOT NULL default '0',
  `intHasArticles` int(12) NOT NULL default '0',
  `intParentID` int(11) NOT NULL default '0',
  `intIsNew` tinyint(1) NOT NULL default '1',
  `ttDateCreated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `cat_keywords` varchar(255) NOT NULL,
  PRIMARY KEY  (`intID`),
  KEY `intParentID` (`intParentID`),
  KEY `varCategory` (`varCategory`),
  KEY `intHasChild` (`intHasChild`),
  KEY `intHasArticles` (`intHasArticles`),
  KEY `intIsNew` (`intIsNew`),
  KEY `ttDateCreated` (`ttDateCreated`),
  KEY `cat_keywords` (`cat_keywords`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=418 ;

 

Any help would be extremly appreciated and I am willing to pay through Paypal for anyones help that can come up with the solution..

 

Thank you

 

xtrax 

Link to comment
Share on other sites

Hello Barand,

 

Thank you for your reply; however I tried that and it didnt work. it could be because i didnt explain myself clearly..

 

The category name is stored in the varCategory in the table tcategories now the other table is tarticles which stores the id of just the category its submitted in which is intCategory and it also has the intAuthorId....

 

so I need the articles the author has submitted which is from tarticles and their intAuthorId as well as the  intCategory  to be linked with the tcategories table varCategory and intParentID to match the tarticles  intCategory  to pull the varname...

 

I am willing to pay you for your time or make a donation to the website for any help.

 

I await your reply

 

xtrax

Link to comment
Share on other sites

I didnt get any error Barand, however it didnt pull the Category name that the author has submitted to...

 

 

I added the SQL statement you gave me to try

 

 

$sql = "SELECT a.intAuthorId, c.varCategory, p.varCategory as parent
FROM tarticles a
    INNER JOIN tcategories c ON a.intCategory = c.intId
    LEFT JOIN tcategories p c.intParentId = p.intId  WHERE intAuthorId = '$author_ID' AND intStatus = '1' DESC LIMIT 0, 10";

 

then I used the following to try and display the category name

 

$result = $d->fetch($sql);
$cattname = $result['varCategory'];
echo "$cattname\n";

 

But it didnt pull any data...

 

xtrax

Link to comment
Share on other sites

Sure thing

 

here is the tarticle table

 

CREATE TABLE IF NOT EXISTS `tarticles` (
  `intId` int(11) NOT NULL auto_increment,
  `intAuthorId` int(11) NOT NULL default '0',
  `intCategory` int(11) NOT NULL default '0',
  `varArticleTitle` varchar(255) NOT NULL default '',
  `textSummary` mediumtext NOT NULL,
  `varKeywords` varchar(255) NOT NULL default '',
  `textResource` mediumtext NOT NULL,
  `intStatus` tinyint(1) NOT NULL default '0',
  `ttSubmitDate` date NOT NULL,
  `word_count` mediumint(9) NOT NULL default '0',
  `SubmittedBy` varchar(50) NOT NULL default 'live',
  `declined` int(1) NOT NULL default '0',
  `hits` varchar(12) NOT NULL,
  `views` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`intId`),
  UNIQUE KEY `varArticleTitle` (`varArticleTitle`),
  KEY `intCategory` (`intCategory`),
  KEY `intAuthorId` (`intAuthorId`),
  KEY `varKeywords` (`varKeywords`),
  KEY `ttSubmitDate` (`ttSubmitDate`),
  KEY `word_count` (`word_count`),
  KEY `SubmittedBy` (`SubmittedBy`),
  KEY `intStatus` (`intStatus`),
  KEY `declined` (`declined`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1231 ;

 

INSERT INTO `tarticles` (`intId`, `intAuthorId`, `intCategory`, `varArticleTitle`, `textSummary`, `varKeywords`, `textResource`, `intStatus`, `ttSubmitDate`, `word_count`, `SubmittedBy`, `declined`, `hits`, `views`) VALUES
(1, 19, 94, 'Online Heavy Equipment Rental: Things To Remember', 'These days, there are a lot of people who prefer to undertake home improvement projects on their own. While this is fine as far as simple home improvement goes, there are some building or construction projects that you may have to call in the pros for and consider construction or heavy <a href="https://www.hertzequip.com">equipment rental</a>. Besides, you won''t have difficulty finding a rental company to work with.', 'equipment rental,rental equipment,excavator,rental,equipment', 'If you want to know more about <a href="https://www.hertzequip.com">rental equipment</a> then visit https://www.hertzequip.com for more details.', 1, '2012-06-19', 431, 'USER', 0, '', 35);

 

Here is a tcategory table sample

 

CREATE TABLE IF NOT EXISTS `tcategories` (
  `intID` int(11) NOT NULL auto_increment,
  `varCategory` varchar(200) NOT NULL default '',
  `textDescription` mediumtext NOT NULL,
  `intHasChild` int(11) NOT NULL default '0',
  `intHasArticles` int(12) NOT NULL default '0',
  `intParentID` int(11) NOT NULL default '0',
  `intIsNew` tinyint(1) NOT NULL default '1',
  `ttDateCreated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `cat_keywords` varchar(255) NOT NULL,
  PRIMARY KEY  (`intID`),
  KEY `intParentID` (`intParentID`),
  KEY `varCategory` (`varCategory`),
  KEY `intHasChild` (`intHasChild`),
  KEY `intHasArticles` (`intHasArticles`),
  KEY `intIsNew` (`intIsNew`),
  KEY `ttDateCreated` (`ttDateCreated`),
  KEY `cat_keywords` (`cat_keywords`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=418 ;

 

Here is the insert data for tcategories

 

INSERT INTO `tcategories` (`intID`, `varCategory`, `textDescription`, `intHasChild`, `intHasArticles`, `intParentID`, `intIsNew`, `ttDateCreated`, `cat_keywords`) VALUES
(1, 'Arts', 'root', 9, 4, 0, 1, '2012-07-14 08:43:07', 'arts,classics,painting,sculpture,artistic works');

 

Let me know if you need anything else

 

xtrax

Link to comment
Share on other sites

No problem, if I'm guessing right, you want to simply grab the names of categories that any given author has posted to? So if authorID 19 has posted an article in 5 different categories, you want to get the names of those 5 categories?

 

It might be easier, but I'd suggest against giving just anyone cPanel access... or at least making a backup first ;)

 

I guess we're both staffers here, so it's not like we're ENTIRELY random.

 

Did I clarify what you wanted above?

Link to comment
Share on other sites

Yes exactly what I am after - and I checked and noticed you both were staffers so I figured it would be safe enough to grant access as it may make things easier

 

try this

 

INSERT INTO `tarticles` (`intId`, `intAuthorId`, `intCategory`, `varArticleTitle`, `textSummary`, `varKeywords`, `textResource`, `intStatus`, `ttSubmitDate`, `word_count`, `SubmittedBy`, `declined`, `hits`, `views`) VALUES
(1, 110, 24, 'Competently Manage Your Internet Marketing Time', 'Time management is crucial for a positive outcome when pursuing Internet marketing. When you do not devote sufficient time to managing your internet marketing scheme, you could be jeopardizing your chance for a big accomplishment.', 'Business,home business,network marketing,online marketing,affiliate marketing,marketing,advertising,internet business,mlm', 'James Steele is a expert blogger known for writing on a varitey of subjects. His high-quality work can be seen at <a href="http://askmarcbarrett.com/mlm-reviews/qivana-review-whats-up-in-mlm-nutrition-land-have-jerry-campisi-and-mark-yarnell-lost-their-minds">operation Qivana</a> and on <a href="http://askmarcbarrett.com/mlm-marketing/ibuzzpro-review-is-ibuzz-pro-a-low-tech-mlm-lead-bonanza-or-outdated-tool">Ibuzzpro recruiting</a>', 1, '2012-07-17', 474, 'USER', 0, '', 4);

 

now the category

 

INSERT INTO `tcategories` (`intID`, `varCategory`, `textDescription`, `intHasChild`, `intHasArticles`, `intParentID`, `intIsNew`, `ttDateCreated`, `cat_keywords`) VALUES (24, 'Business', 'root', 10, 119, 0, 1, '2012-07-18 13:00:50', 'businesses,business,online business,Mom and Pop,cartel,corporation,factory,firm,fly-by-night operation,institution,market,megacorp,mill,monopoly,partnership,shoestring operation,shop,store'),

 

the category is ia 24 which should be buisness

 

xtrax

Link to comment
Share on other sites

Try this SQL

 

SELECT DISTINCT
  cat.varCategory
FROM
  tarticles art
  LEFT JOIN
    tcategories cat
  ON
    art.intCategory = cat.intID
WHERE
  art.intAuthorId = 19

 

replace 19 with the AuthorID you want.

Link to comment
Share on other sites

Hummm ok Im not getting any error message yet its still not showing anything ...

 

I am using this to pull the data

 

$sql = "SELECT DISTINCT(cat.varCategory) FROM tarticles art LEFT JOIN tcategories cat ON art.intCategory = cat.intID WHERE art.intAuthorId = '$author_ID'  AND intStatus = '1' DESC LIMIT 0, 10";

$result = $d->fetch($sql);
$cattname = $result['cat.varCategory'];
echo "$cattname\n";

 

and i tried this as well

 

$sql = "SELECT DISTINCT(cat.varCategory) FROM tarticles art LEFT JOIN tcategories cat ON art.intCategory = cat.intID WHERE art.intAuthorId = '$author_ID'  AND intStatus = '1' DESC LIMIT 0, 10";
$result = $d->fetch($sql);
$cattname = $result['varCategory'];
echo "$cattname\n";

 

turned on error reporting and its not reporting any errors

 

xtrax  :-[

Link to comment
Share on other sites

Well, DESC needs an ORDER BY clause, I think. And intStatus should be referenced to it's table.

 

Ordering while using DISTINCT might create a temporary table, so it might be better to sort in PHP for such simple results.

 

Here's my tables and sample data

--
-- Table structure for table `tarticles`
--

CREATE TABLE IF NOT EXISTS `tarticles` (
  `intId` int(11) NOT NULL AUTO_INCREMENT,
  `intAuthorId` int(11) NOT NULL DEFAULT '0',
  `intCategory` int(11) NOT NULL DEFAULT '0',
  `varArticleTitle` varchar(255) NOT NULL DEFAULT '',
  `textSummary` mediumtext NOT NULL,
  `varKeywords` varchar(255) NOT NULL DEFAULT '',
  `textResource` mediumtext NOT NULL,
  `intStatus` tinyint(1) NOT NULL DEFAULT '0',
  `ttSubmitDate` date NOT NULL,
  `word_count` mediumint(9) NOT NULL DEFAULT '0',
  `SubmittedBy` varchar(50) NOT NULL DEFAULT 'live',
  `declined` int(1) NOT NULL DEFAULT '0',
  `hits` varchar(12) NOT NULL,
  `views` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`intId`),
  UNIQUE KEY `varArticleTitle` (`varArticleTitle`),
  KEY `intCategory` (`intCategory`),
  KEY `intAuthorId` (`intAuthorId`),
  KEY `varKeywords` (`varKeywords`),
  KEY `ttSubmitDate` (`ttSubmitDate`),
  KEY `word_count` (`word_count`),
  KEY `SubmittedBy` (`SubmittedBy`),
  KEY `intStatus` (`intStatus`),
  KEY `declined` (`declined`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1083 ;

--
-- Dumping data for table `tarticles`
--

INSERT INTO `tarticles` (`intId`, `intAuthorId`, `intCategory`, `varArticleTitle`, `textSummary`, `varKeywords`, `textResource`, `intStatus`, `ttSubmitDate`, `word_count`, `SubmittedBy`, `declined`, `hits`, `views`) VALUES
(1, 19, 94, 'Online Heavy Equipment Rental: Things To Remember', 'These days, there are a lot of people who prefer to undertake home improvement projects on their own. While this is fine as far as simple home improvement goes, there are some building or construction projects that you may have to call in the pros for and consider construction or heavy <a href="https://www.hertzequip.com">equipment rental</a>. Besides, you won''t have difficulty finding a rental company to work with.', 'equipment rental,rental equipment,excavator,rental,equipment', 'If you want to know more about <a href="https://www.hertzequip.com">rental equipment</a> then visit https://www.hertzequip.com for more details.', 1, '2012-06-19', 431, 'USER', 0, '', 35);

-- --------------------------------------------------------

--
-- Table structure for table `tcategories`
--

CREATE TABLE IF NOT EXISTS `tcategories` (
  `intID` int(11) NOT NULL AUTO_INCREMENT,
  `varCategory` varchar(200) NOT NULL DEFAULT '',
  `textDescription` mediumtext NOT NULL,
  `intHasChild` int(11) NOT NULL DEFAULT '0',
  `intHasArticles` int(12) NOT NULL DEFAULT '0',
  `intParentID` int(11) NOT NULL DEFAULT '0',
  `intIsNew` tinyint(1) NOT NULL DEFAULT '1',
  `ttDateCreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `cat_keywords` varchar(255) NOT NULL,
  PRIMARY KEY (`intID`),
  KEY `intParentID` (`intParentID`),
  KEY `varCategory` (`varCategory`),
  KEY `intHasChild` (`intHasChild`),
  KEY `intHasArticles` (`intHasArticles`),
  KEY `intIsNew` (`intIsNew`),
  KEY `ttDateCreated` (`ttDateCreated`),
  KEY `cat_keywords` (`cat_keywords`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=418 ;

--
-- Dumping data for table `tcategories`
--

INSERT INTO `tcategories` (`intID`, `varCategory`, `textDescription`, `intHasChild`, `intHasArticles`, `intParentID`, `intIsNew`, `ttDateCreated`, `cat_keywords`) VALUES
(94, 'Arts', 'root', 9, 4, 0, 1, '2012-07-18 21:42:13', 'arts,classics,painting,sculpture,artistic works');

 

And my query/results

 

mysql> SELECT DISTINCT
    ->   cat.varCategory
    -> FROM
    ->   tarticles art
    ->   LEFT JOIN
    ->     tcategories cat
    ->   ON
    ->     art.intCategory = cat.intID
    -> WHERE
    ->   art.intAuthorId = 19
    ->   AND
    ->   art.intStatus = 1
    -> ORDER BY
    ->   cat.varCategory DESC;
+-------------+
| varCategory |
+-------------+
| Arts        |
+-------------+
1 row in set (0.00 sec)

 

Is that what you expect?

Link to comment
Share on other sites

I ran the sql query in phpmyadmin for one of our authors that has a ton of articles and it pulls the categories they submitted to...

 

So I tried on another author with less articles to insure it wasnt pulling all of them and it pulled just the 7 categories so the query is working!!!

 

However its not showing the results on the page! which is weird

 

is this correct...

 

$sql = "SELECT DISTINCT(cat.varCategory) FROM tarticles art LEFT JOIN tcategories cat ON art.intCategory = cat.intID WHERE art.intAuthorId = '$author_ID' AND art.intStatus = '1'";
$result = $d->fetch($sql);
$cattname = $result['varCategory'];
echo "$cattname\n";

 

xtrax

Link to comment
Share on other sites

I didnt get any error Barand, however it didnt pull the Category name that the author has submitted to...

 

 

I added the SQL statement you gave me to try

 

 

$sql = "SELECT a.intAuthorId, c.varCategory, p.varCategory as parent
FROM tarticles a
    INNER JOIN tcategories c ON a.intCategory = c.intId
    LEFT JOIN tcategories p c.intParentId = p.intId  WHERE intAuthorId = '$author_ID' AND intStatus = '1' DESC LIMIT 0, 10";

 

then I used the following to try and display the category name

 

$result = $d->fetch($sql);
$cattname = $result['varCategory'];
echo "$cattname\n";

 

But it didnt pull any data...

 

xtrax

 

You need to execute the statement before you canfetch data

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.