is there way insert multiple items database row return results , counter part commas?
for instance:
post postid url hits 1 http://google.com,http://facebook.com 35,20
so post 1 has 2 urls in it, google has 35 hits, while facebook has 20. want show result using like:
$query = $db->query_first(" select * ". table_prefix ."post url = '" . $drc_url . "' ");
but want pull 1 url @ time corresponding hits value. possible? if can point me in right direction?
right php looks like:
$query = $db->query_first("select * ". table_prefix ."post url = '" . $drc_url . "'"); if (!$query){ $db->query_write("insert ". table_prefix ."redirect (url, hits) values ('" . $drc_url . "', 1)"); } else { $db->query_write("update ". table_prefix ."redirect set hits = hits + 1 url = '" . $drc_url . "'"); }
but if theyre in same $postid want them put in same row
see comments in code.
$drc_url = "http://google.com"; /* insert/update */ $query = $db->query("select * ". table_prefix ."post url '%" . $drc_url . "%'"); if ( $query->num_rows > 0 ) { // url can in multiple rows - go through each row while( $row = $query->fetch_assoc() ) { //find url position/index within list //if it's possible urls separated else comma (for example "comma , space", "space", etc), need add functionality able find them correctly $urls = explode( ',', $row[ 'url' ] ); $index = array_search( $drc_url, $urls ); //same previous comment goes hits row $hits = explode( ',', $row[ 'hits' ] ); //increment hits number on correct value $hits[$index]++; //update hits (put them string array incremented value) - use unique identificator in clause $db->query("update ". table_prefix ."redirect set hits = '" . implode( ',', $hits ) . "' postid = '" . $row[ 'postid' ] . "'"); } } else { // url not found - insert $db->query("insert ". table_prefix ."redirect (url, hits) values ('" . $drc_url . "', 1)"); } /* select */ //same functionality above, not updating $query = $db->query("select * ". table_prefix ."post url '%" . $drc_url . "%'"); while( $row = $query->fetch_assoc() ) { $urls = explode( ',', $row[ 'url' ] ); $index = array_search( $drc_url, $urls ); $hits = explode( ',', $row[ 'hits' ] ); echo "url " . $drc_url . " has " . $hits[$index] . "hits"; }
but mark baker wrote in comment, better change db structure , build on new structure.
Comments
Post a Comment