Jump to content

Recommended Posts

HI everyone,

 

I keep coming up against this.

 

I am trying to use the results of a query as an array, currently coded thus:

 

$query="SELECT moj_genre FROM genres INNER JOIN genrelinkcd ON genres.genre_id=genrelinkcd.genre_id WHERE genrelinkcd.moj_id='$id'";

$result=mysql_query($query);

$orig_genres=array();

while($row = mysql_fetch_array($result))
{
echo "$row[0]".", "; // Printing it on screen at the same time which works fine.
$orig_genres[] =$row[0]; // This is where I'm trying to create the array but maybe there's an even shorter cut?

}

 

When I try and use the following code later on the same page:

 

$missing_genres=array_diff($orig_genres, $new_genres);

 

I get the error message:

 

Warning: array_diff() [function.array-diff]: Argument #1 is not an array in C:\web\mojo\php-files\edit_mojo_cd.php on line 419

 

What do I keep doing wrong? I am constantly falling down when I'm trying to process mysql results.

 

Cheers,

KN

Link to comment
https://forums.phpfreaks.com/topic/173837-use-result-of-query-as-an-array/
Share on other sites

do a print_r on the $orig_genres before you call that function. see what it holds.

 

this line

$orig_genres[] = $row[0];

 

is correct, in that it will push the value of $row[0] in the $orig_games array. Idk what else is happening to it afterwards tho, so try a print_r and maybe post the code after that while loop

new_genres is fine as well.

 

Here's the rest of the code. The MySQL queries that follow the functions are still a work in progress, but the errors are occuring on the lines before.

 

$query="SELECT moj_genre FROM genres INNER JOIN genrelinkcd ON genres.genre_id=genrelinkcd.genre_id WHERE genrelinkcd.moj_id='$id'";
$result=mysql_query($query);
// $num_rows=mysql_num_rows($result);
// $count=1;


$orig_genres=array();

while($row = mysql_fetch_array($result))
{
echo "$row[0]".", ";
$orig_genres[] =$row[0];

}
// print_r($orig_genres);

?>	

">




</tr> 


<tr> 

    <td colspan=2> 

      <input type="Submit" name="submit" value="Update"> 

    </td> 

</tr> 

</form> 

</table> 

<?php 

    } 

    // no result returned 

    // print error message 

    else 

    { 

        echo '<font size=-1>That instance could not be located in the database.</font>'; 

    } 

} 


else 

{ 

    // set up error list array 

    $errorList = array(); 

    $moj_title = $_POST['moj_title']; 

$moj_issue = $_POST['moj_issue'];

$moj_day = $_POST['day'];

$moj_month = $_POST['month'];

$moj_year = $_POST['year'];

    $moj_summary = $_POST['moj_summary']; 

$moj_genre = $_POST['moj_genre'];

    $moj_id = $_POST['id']; 


        
// Check for errors

if (trim($_POST['moj_title']) == '')
{
	$errorList[] = 'Invalid entry: Title';
}


if (trim($_POST['moj_issue']) == '')
{
	$errorList[] = 'Invalid entry: Issue (remember - not validating numeric entry yet)';
}

if (trim($_POST['moj_summary']) == '')
{
	$errorList[] = 'Invalid entry: Summary';
}

if (trim($_POST['moj_genre']) == '')
{
	$errorList[] = 'You must enter at least one genre tag';
}

// ultimately the day will be checked against the month i.e. no 30th Feb. Can maybe do this with 'minus' on unixtime
if (trim($_POST['day']) == '' && ($freq == "daily"))
{
	$errorList[] = 'Invalid entry: Day of date';
	}

if (trim($_POST['month']) == '' && (($freq == "daily") || ($freq == "monthly")))
{
	$errorList[] = 'Invalid entry: Month of date';
	}		

if (trim($_POST['year']) == '' && ((($freq == "daily") || ($freq == "monthly") || ($freq == "yearly"))))
{
	$errorList[] = 'Invalid entry: Year of date';
	}		
    // if none found... 

    if (sizeof($errorList) == 0) 

    { 

        // open database connection 

        $connection = mysql_connect($host, $user, $pass)

or die ('Unable to connect!'); 

        // select database 

        mysql_select_db($db)

or die ('Unable to select database!'); 


// This works for the text based date for now as preceding spaces appear to be stripped
// $date = "$moj_day"." "."$moj_month"." "."$moj_year";  



// if monthly turn $day into 01 and use $month and $year for $unixdate
// $day = '8';
// $month = 'February';
// $year = '1978';

// NB: FREQUENCY $freq MUST BE POSTED!!!
$freq = 'monthly';

if ($freq == 'daily') 
{
$moj_date = "$moj_day"." "."$moj_month"." "."$moj_year";
}

if ($freq == 'monthly')
{
$moj_day = '1';
$moj_date = "$moj_month"." "."$moj_year";
}
// if yearly, turn $day into 01 and $month into 01 and use $year for $unixdate


if ($freq == 'yearly')
{
$moj_day = '1';
$moj_month = 'January';
$moj_date = "$moj_year";}


$dateforunix = "$moj_day-$moj_month-$moj_year + 1 hour";
$unixdate = strtotime($dateforunix);

// echo $dateforunix; ?><br><?php
// echo $unixdate; ?><br><?php
// echo $date;


// generate and execute query
$query = "UPDATE mojocd SET moj_title = '$moj_title', moj_date = '$moj_date', moj_issue = '$moj_issue', moj_summary = '$moj_summary', unix_timestamp = '$unixdate' WHERE moj_id = '$moj_id'";

$result = mysql_query($query)
or die ("Error in Query: $query. " . mysql_error());


$new_genres = explode(",", $moj_genre);
print_r($new_genres);

$missing_genres=array_diff($orig_genres, $new_genres);

//now we have to count the occurances of each $missing_genre in genrelinkcd. If it is 1 it needs to be deleted from genres. Whatever the outcome, $missing_genre needs to be deleted where moj_id=$moj_id


foreach ($missing_genres as $row)
{

//connect to database??

$query="SELECT genre_id FROM genrelinkcd and genres INNER JOIN genrelinkcd ON genres.genre_id=genrelinkcd.genre_id WHERE genres.moj_genre='$row'";

$result = mysql_query($query);
$amount = mysql_num_rows($result);    

//Get amount as $amount

if ($amount == 1)
{

// If it is the only instance, delete the tag from the 'genres' table

$query="DELETE * FROM genres WHERE moj_genre='$row'";
$result = mysql_query($query) or die;

}

// Delete the genres that are now missing from genrelinkcd

// $query="DELETE * FROM genrelinkcd WHERE genrelinkcd INNER JOIN genres ON genres.genre_id = genrelinkcd.genre_id AND genres.moj_genre='$row' 

$query="DELETE * FROM genrelinkcd WHERE genrelinkcd.genre_id = (SELECT genre_id FROM genres WHERE moj_genre='$row') AND	genrelinkcd.moj_id='$id'";
$result = mysql_query($query) or die;
}


//establish which tags are new. Simply reverse the strings in array_diff!

$added_genres=array_diff($new_genres, $orig_genres);

foreach ($added_genres as $row)
{
$query="SELECT * FROM genres WHERE moj_genre='$row'";

$result = mysql_query($query);
$amount = mysql_num_rows($result);    

//Get amount as $amount

if ($amount == 0)
{

// If it does not already exist in genres, add it to genres

$query="INSERT INTO genres (moj_genre) VALUE ('$row')";
$result = mysql_query($query) or die;
}

// put a record in genrelinkcd whatever the result

$query="INSERT INTO genrelinkcd (genre_id, moj_id) VALUES (genres.genre_id WHERE genres.moj_genre = '$row'), '$id'";
$result = mysql_query($query) or die;

}
?> 
"

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.