Hi i wanted to loop the options list of the select tag inside that echo statement from my database. This file out puts the content to a javascript code. and this output is also javascript. any one help me how can i loop that options in select tag from the database. when i tried to use a for loop it gives an error. the javascript doesnt work.

require_once("connections.php");

if(!$_POST['img']) die("There is no such product!");

$img=mysql_real_escape_string(end(explode('/',$_POST['img'])));
$q_result=mysql_query("SELECT * FROM tbl_items WHERE item_photo='".$img."'");
$row=mysql_fetch_assoc($q_result);
$q_rows = mysql_num_rows($q_result);

echo '{status:1,id:'.$row['itid'].',price:'.$row['item_price'].',txt:\'\
\
<table width="100%" id="table_'.$row['itid'].'">\
  <tr>\
    <td width="20%">'.$row['item_title'].'</td>\
    <td width="10%"><select name="'.$row['itid'].'_cnt" id="'.$row['itid'].'_cnt" onchange="change('.$row['itid'].');">\
	<option value="1">1</option>\
	</select>\
	\</td>\
    <td width="15%"><select name="'.$row['itid'].'_cnt" id="'.$row['itid'].'_cnt" onchange="change('.$row['itid'].');">\
	<option value="1">Daily</option>\
	<option value="2">Weekly</option>\
	<option value="3">Monthly</option>\
	<option value="4">1 Year</option>\
	<option value="5">2 Years</option>\
	<option value="6">3 Years</option>\
	</select>\
	\
	</td>\
	<td width="15%"><a href="#" onclick="remove('.$row['itid'].');return false;" class="remove">Remove</a></td>\
  </tr>\
</table>\'}';

where is your javascript located, could you copy the whole script?

var purchased=new Array();
var totalprice=0;

$(document).ready(function(){
	
	
	$(".product_item img").draggable({
	
	containment: 'document',
	opacity: 0.6,
	revert: 'invalid',
	helper: 'clone',
	zIndex: 100
	
	});

	$(".drop-here").droppable({
	
			drop:
					function(e, ui)
					{
						var param = $(ui.draggable).attr('src');
						
						if($.browser.msie && $.browser.version=='6.0')
						{
							param = $(ui.draggable).attr('style').match(/src=\"([^\"]+)\"/);
							param = param[1];
						}

						addlist(param);
						
					}
					
	
	});

});


function addlist(param)
{
	$.ajax({
	type: "POST",
	url: "ajax/addtocart.php",
	data: 'img='+encodeURIComponent(param),
	dataType: 'json',
	beforeSend: function(x){$('#ajax-loader').css('visibility','visible');},
	success: function(msg){
		
		$('#ajax-loader').css('visibility','hidden');
		if(parseInt(msg.status)!=1)
		{
			return false;
		}
		else
		{
			var check=false;
			var cnt = false;
			
			for(var i=0; i<purchased.length;i++)
			{
				if(purchased[i].id==msg.id)
				{
					check=true;
					cnt=purchased[i].cnt;
					
					break;
				}
			}
			
			if(!cnt)
				$('#item-list').append(msg.txt);
				
			if(!check)
			{
				purchased.push({id:msg.id,cnt:1,price:msg.price});
			}
			else
			{
				if(cnt>=3) return false;
				
				purchased[i].cnt++;
				$('#'+msg.id+'_cnt').val(purchased[i].cnt);
			}
			
			totalprice+=msg.price;
			update_total();

		}
		
		$('.tooltip').hide();
	
	}
	});
}

function findpos(id)
{
	for(var i=0; i<purchased.length;i++)
	{
		if(purchased[i].id==id)
			return i;
	}
	
	return false;
}

function remove(id)
{
	var i=findpos(id);

	totalprice-=purchased[i].price*purchased[i].cnt;
	purchased[i].cnt = 0;

	$('#table_'+id).remove();
	update_total();
}

function change(id)
{
	var i=findpos(id);
	
	totalprice+=(parseInt($('#'+id+'_cnt').val())-purchased[i].cnt)*purchased[i].price;
	
	purchased[i].cnt=parseInt($('#'+id+'_cnt').val());
	update_total();
}

function update_total()
{
	if(totalprice)
	{
		$('#total').html('total: $'+totalprice);
		$('a.button').css('display','block');
		$('#cart-text').css('display','none');
	}
	else
	{
		$('#total').html('');
		$('a.button').hide();
		$('#cart-text').css('display','block');
	}
}

this file calls to that php file. and that php files out puts to this javascript file. its an ajax call. see that "ajax/addtocart.php". that file is the file i have given in first post.

because its $(document).ready(); it will only effect all elements, that are there after the document loaded, and not the elements, that are included after the document loaded like via ajax... you could add the nessessary functions in the ajax function I think

The addtocart.php(the file i have given above) loads every time i added something to the cart. and i want to loop the <select> tag <option> list in that file from a database. now i have to manually add the <options> there. and when i simply write a loop there the javascript doesnt work. any help?

when i do something like this, the javascript code doesnt work.

require_once("../Connection/connection.php");

if(!$_POST['img']) die("There is no such product!");

$img=mysql_real_escape_string(end(explode('/',$_POST['img'])));
$q_result=mysql_query("SELECT * FROM tbl_items WHERE item_photo='".$img."'");
$row=mysql_fetch_assoc($q_result);
$q_rows = mysql_num_rows($q_result);


echo '{status:1,id:'.$row['itid'].',price:'.$row['item_price'].',txt:\'\
\
<table width="100%" id="table_'.$row['itid'].'">\
  <tr>\
    <td width="20%">'.$row['item_title'].'</td>\
    <td width="10%"><select name="'.$row['itid'].'_cnt" id="'.$row['itid'].'_cnt" onchange="change('.$row['itid'].');">\
	'.for($i=1; $i<=10; $i++){.'<option value="1">'.$i.'</option>'.}.'\
	\
	</select>\
	\</td>\
    <td width="15%"><select name="'.$row['itid'].'_cnt" id="'.$row['itid'].'_cnt" onchange="change('.$row['itid'].');">\
	<option value="1">Daily</option>\
	<option value="2">Weekly</option>\
	<option value="3">Monthly</option>\
	<option value="4">1 Year</option>\
	<option value="5">2 Years</option>\
	<option value="6">3 Years</option>\
	</select>\
	\
	</td>\
	<td width="15%"><a href="#" onclick="remove('.$row['itid'].');return false;" class="remove">Remove</a></td>\
  </tr>\
</table>\'}';

You don't do it in the echo. You break the one echo statement into two echos with a foreach in between, something like:

echo '{status:1,id:'.$row['itid'].',price:'.$row['item_price'].',txt:\'\
\
<table width="100%" id="table_'.$row['itid'].'">\
  <tr>\
    <td width="20%">'.$row['item_title'].'</td>\
    <td width="10%"><select name="'.$row['itid'].'_cnt" id="'.$row['itid'].'_cnt" onchange="change('.$row['itid'].');">\
        <option value="1">1</option>\
        </select>\
        \</td>\
    <td width="15%"><select name="'.$row['itid'].'_cnt" id="'.$row['itid'].'_cnt" onchange="change('.$row['itid'].');">';

$options = array("1" => "Daily", "Weekly", "Monthly");
foreach ($options as $key => $value)
{
  echo '        <option value="'.$key.'>$value</option>\\\n';
}

echo '  </select>\
        \
        </td>\
        <td width="15%"><a href="#" onclick="remove('.$row['itid'].');return false;" class="remove">Remove</a></td>\
  </tr>\
</table>\'}';

You can also drop most of your trailing '\', unless you intend your javascript code to be unreadable.

if you want more freedom with an echo you should do:

<?
if(!$_POST[img]){ 
?>

Your echo would go here and therefore you could use " and ' although all PHP would have to go in brackets again <? echo"$somevariable"; ?> 
<? } ?>

You don't do it in the echo. You break the one echo statement into two echos with a foreach in between, something like:

echo '{status:1,id:'.$row['itid'].',price:'.$row['item_price'].',txt:\'\
\
<table width="100%" id="table_'.$row['itid'].'">\
  <tr>\
    <td width="20%">'.$row['item_title'].'</td>\
    <td width="10%"><select name="'.$row['itid'].'_cnt" id="'.$row['itid'].'_cnt" onchange="change('.$row['itid'].');">\
        <option value="1">1</option>\
        </select>\
        \</td>\
    <td width="15%"><select name="'.$row['itid'].'_cnt" id="'.$row['itid'].'_cnt" onchange="change('.$row['itid'].');">';

$options = array("1" => "Daily", "Weekly", "Monthly");
foreach ($options as $key => $value)
{
  echo '        <option value="'.$key.'>$value</option>\\\n';
}

echo '  </select>\
        \
        </td>\
        <td width="15%"><a href="#" onclick="remove('.$row['itid'].');return false;" class="remove">Remove</a></td>\
  </tr>\
</table>\'}';

You can also drop most of your trailing '\', unless you intend your javascript code to be unreadable.

Thanks a lot Fest3er. i spent like 10 hours doing this and u solved it. great. Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.