Jump to content

Recommended Posts

Hello, I try to make some loops, here is my code:

 

	$sourcesnot = mysql_query("SELECT megavideo, novamov, videoweed, movshare, stagevu FROM pm_player_sources WHERE uniq_id = '".$uniqid."'");
//$row4 = mysql_fetch_array( $sourcesnot );
while($row5 = mysql_fetch_array($sourcesnot))
{
	$id = $row5;

	foreach ($id as $k => $v ) {
		if ($v > 0){


			//unset($sources[$v]);
			var_dump($v);

		}
    
         }

}

 

and that's what I get:

 

string(1) "4"

string(1) "4"

string(1) "6"

string(1) "6"

 

but I need only one 4 and one 6 like it is in my database, I think I made a wrong loop and has somebody an idea how to collect them to make an unset() function

Link to comment
https://forums.phpfreaks.com/topic/204205-something-wrong-with-my-loops/
Share on other sites

If you change your script to be:

<?php

$sourcesnot = mysql_query("SELECT megavideo, novamov, videoweed, movshare, stagevu FROM pm_player_sources WHERE uniq_id = '".$uniqid."'");
//$row4 = mysql_fetch_array( $sourcesnot );
while($row5 = mysql_fetch_array($sourcesnot))
{
	echo '<pre>' . print_r($row5,true) . '</pre>'
}
?>

You will see that the function mysql_fetch_array returns each field twice. Use the function mysql_fetch_assoc instead.

 

Ken

My problem is I'm very bad with arrays, just started with. So here now I get 4 and 6 the others are 0, now I want to unset() 4 and 6 more below in the script from the players because the players (keys) 4 and 6 are already used. I thought I make now a new array and unset them with a foreach, I did like that

$arr = array(); 
                $arr[] = $v;

but then I get two arrays like

array(1) {

  [0]=>

  string(1) "4"

}

array(1) {

  [0]=>

  string(1) "6"

}

 

I don't know if it will work just try it that way

I have another array and I want to unset() these keys, that works, testet with one number, but my problem now is I don't get an array

 

	$sourcesnot = mysql_query("SELECT megavideo, novamov, videoweed, movshare, stagevu FROM pm_player_sources WHERE uniq_id = '".$uniqid."'");
//$row4 = mysql_fetch_array( $sourcesnot );
while($row5 = mysql_fetch_assoc($sourcesnot))
{
	$id = $row5;

	foreach ($id as $k => $v ) {
		if ($v > 0){

			//$sourcenot = '['.$v.']';
			//var_dump($v); 
			//unset($sources[$v]);
			$arr = array($v);
                //$arr[] = $v;
			var_dump($arr);

		}
    
         }

}

 

and I get this

 

array(1) {

  [0]=>

  string(1) "4"

}

array(1) {

  [0]=>

  string(1) "6"

}

 

but I always need only one array

Try:

<?php
$sourcesnot = mysql_query("SELECT megavideo, novamov, videoweed, movshare, stagevu FROM pm_player_sources WHERE uniq_id = '".$uniqid."'");
$arr = array();
while($row5 = mysql_fetch_assoc($sourcesnot))
{		
	foreach ($row5 as $k => $v ) {
		if ($v > 0){
                           $arr[] = $v;
		}
	}
}
echo '<pre>' . print_r($arr,true) . '</pre>';
?>

 

Ken

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.