Code: Select all
SELECT tag_idFROM blog_tagsWHERE tag_name = 'exists1'OR tag_name = '!exist'OR tag_name = 'exists2'Thoughts please?
Moderator: General Moderators
Code: Select all
SELECT tag_idFROM blog_tagsWHERE tag_name = 'exists1'OR tag_name = '!exist'OR tag_name = 'exists2'Code: Select all
if(mysql_num_rows($result) == 0) echo "it's empty";JAB Creations wrote:My initial post for this thread contains all the code for the query I have right now.
....JAB Creations wrote:I will be assigning values from the MySQL array to another array. However if a row is missing (and there will almost always be a missing row) then the data won't line up and the whole array code will be in vain.
The main point of doing this is to avoid making multiple MySQL queries...there absolutely has to be a way to return a row with something...anything even if one does not exist from the database. Something *has* to be returned...allowing one query total...otherwise I'll end up having to put a query in to a loop and executing as many times as the loop executes! I'd really like to avoid that and learn a new trick at the same time if at all possible. Plus this will help me get more comfortable with arrays which has started to become a personal campaign of mine.
Code: Select all
<?php
include("_0_header_02_mysql.php");
$thread_tags_new = $_POST['tags_new'];
$thread_tags_old = $_POST['tags_old'];
$tags_array_new = explode(', ',$thread_tags_new);
$tags_array_old = explode(', ',$thread_tags_old);
$tags_remove = array_diff($tags_array_old, $tags_array_new);
$result = mysql_query("SELECT tag_id FROM blog_tags WHERE tag_name='".implode("' OR tag_name='",$tags_remove)."'");
//$row = mysql_fetch_array($result);
$count = count($tags_remove);
echo '<div>count == '.$count.'</div>';
echo '<div>';
$i = 0;
while ($i <= $count && $row = mysql_fetch_array($result)) {
//print_r($row[0]);
echo '<div>'.$i.' == '; print_r($row[0]); echo '</div>';
$i++;
$new_array[$i] = $row[0];
}
mysql_free_result($result);
echo '</div>';
//print_r($new_array);
//$query2 = "DELETE FROM blog_xhref_tags WHERE xhref_thread_id='1' AND (xhref_tag_id='".implode("' OR xhref_tag_id='",$tags_remove)."')";
echo "DELETE FROM blog_xhref_tags WHERE xhref_thread_id='1' AND (xhref_tag_id='";
print_r(implode("' OR xhref_tag_id='",$new_array));
echo "')";
?>
<form action="<?php
if ($_GET['tags'] == "1") {echo 'test5.php?tags=2';}
else if ($_GET['tags'] == "2" || !isset($_GET['tags'])) {echo 'test5.php?tags=1';}
?>" method="post">
<fieldset>
<div><label for="tags_new">Tags</label><input id="tags_new" name="tags_new" style="width: 512px;" value="<?php
$tags1 = 'application/xhtml+xml, XML, XHTML, CSS, Flash, ASP.NET, Java';
$tags2 = 'application/xhtml+xml, XML, XHTML, CSS, JavaScript, PHP, MySQL';
if ($_GET['tags'] == "1") {echo $tags1;}
else if ($_GET['tags'] == "2" || !isset($_GET['tags'])) {echo $tags2;}
?>" /></div>
<input name="tags_old" type="hidden" value="<?php
$tags1 = 'application/xhtml+xml, XML, XHTML, CSS, Flash, ASP.NET, Java';
$tags2 = 'application/xhtml+xml, XML, XHTML, CSS, JavaScript, PHP, MySQL';
if ($_GET['tags'] == "2") {echo $tags1;}
else if ($_GET['tags'] == "1" || !isset($_GET['tags'])) {echo $tags2;}
?>" />
<div><input type="submit" /></div>
</fieldset>
</form>Code: Select all
---- Table structure for table `blog_tags`-- CREATE TABLE IF NOT EXISTS `blog_tags` ( `tag_id` INT(6) NOT NULL AUTO_INCREMENT, `tag_name` VARCHAR(64) COLLATE utf8_unicode_ci NOT NULL, `tag_name_base` VARCHAR(64) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`tag_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=39 ; ---- Dumping data for table `blog_tags`-- INSERT INTO `blog_tags` (`tag_id`, `tag_name`, `tag_name_base`) VALUES(1, 'application/xhtml+xml', 'applicationxhtmlxml'),(2, 'XML', 'xml'),(3, 'XHTML', 'xhtml'),(4, 'CSS', 'css'),(5, 'JavaScript', 'javascript'),(6, 'Example Tag', 'example_tag'),(7, 'Tag1', 'tag1'),(8, 'Tag2', 'tag2'),(9, 'Tag3', 'tag3'),(19, 'MySQL', 'mysql'),(18, 'PHP', 'php'),(36, 'Flash', 'flash'),(37, 'ASP.NET', 'aspnet'),(38, 'Java', 'java'); SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";-- Database: `jabcreat_members` -- Table structure for table `blog_xhref_tags` CREATE TABLE IF NOT EXISTS `blog_xhref_tags` ( `xhref_id` INT(6) NOT NULL AUTO_INCREMENT, `xhref_tag_id` INT(6) NOT NULL, `xhref_thread_id` INT(6) NOT NULL, PRIMARY KEY (`xhref_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=31 ; -- Dumping data for table `blog_xhref_tags` INSERT INTO `blog_xhref_tags` (`xhref_id`, `xhref_tag_id`, `xhref_thread_id`) VALUES(1, 1, 1),(2, 2, 1),(3, 3, 1),(4, 4, 1),(5, 2, 2),(6, 3, 3),(7, 4, 4),(8, 3, 2),(9, 2, 3),(10, 1, 4),(11, 3, 4),(12, 3, 6),(13, 3, 5),(14, 3, 7),(28, 34, 1),(27, 33, 1),(29, 5, 1);Code: Select all
$thread_tags_new = $_POST['tags_new'];
$thread_tags_old = $_POST['tags_old'];
$tags_array_new = explode(', ',$thread_tags_new);
$tags_array_old = explode(', ',$thread_tags_old);
$tags_remove = array_diff($tags_array_old, $tags_array_new);
$tags_add = array_diff($tags_array_new,$tags_array_old); //By switching the order of the arrays, we diff the opposite way
if(count($tags_add) > 0 ) { //If there are tags to add, check if they exist in the tag table
$query = "SELECT tag_name FROM blog_tags WHERE tag_name='".implode("' OR tag_name='",$tags_add). "'";
$result = mysql_query($query);
$existing_tags = array();
while($row = mysql_fetch_assoc($result) ) {
$existing_tags[] = $row['tag_name'];
}
$tags_add_tagtable = array_diff($tags_add,$existing_tags); //Filter to the tags that don't exist yet
foreach($tags_add_tagtable as $newtag) { //Iterate over non-existing tags and add to tag table
$query = "INSERT INTO blog_tags (tag_name) VALUES ('" . mysql_real_escape_string($newtag) . "')";
mysql_query($query);
}
}