Jump to content

Echo out results comma and apostrophe delimited


Recommended Posts

Hello,

 

I am new to php and stuck on this msqli function.

 

I would like to echo out all the results divided by commas and apostrophe.  Example:  "23232","324231","342533"

 

With the last one not having a comma.

 

Here is what I came up with and not sure why I am getting errors.

 

I think this is the issue but not sure: $list.= $result->'"'.video_code.'",';

 

 

Thanks.

$sql = 'SELECT video_code FROM yt_videos WHERE yt_video_item_id="'.$vidid.'" ORDER BY RAND() LIMIT 10'; 
			
$query = mysqli_query($db_conx, $sql);
			
			
$list='';
while ($result = $query ->fetch_object()) {
$list.= $result->'"'.video_code.'",';
}
					
echo $list=substr($list,0,-1); 
Link to comment
Share on other sites

Thanks for the reply. I don't quite understand how to implement the join function in my code.

 

I think I am close other wise as I am no longer getting an error but not quite the result I need.

 

When I use this code it works:

$list.= $result->video_code.'",';

Resulting in 2343234",345353",43533534"

 

How do I write that line to include a " before each row result too.  Looking for "2343234","345353","43533534" 

 

Thanks.

Link to comment
Share on other sites

Ok I tried the join function but now I am getting an error: Warning: join() [function.join]: Invalid arguments passed in...

 

Here is my code.  I just don't understand mysqli, I just have some experience and mysql.

$sql='SELECT video_code FROM yt_videos WHERE yt_video_item_id="'.$vidid.'" ORDER BY RAND() LIMIT 10';
$result=mysqli_query($db_conx,$sql);
			
mysqli_free_result($result);
echo '"' . join('","', $result) . '"';
Edited by solidrocketfuel
Link to comment
Share on other sites

Thanks Fastol.  That helped.  Now its only echoing one result.

 

Here is my code now.  No errors!! Getting close! :)

 

$sql = 'SELECT video_code FROM yt_videos WHERE yt_video_item_id="'.$vidid.'" ORDER BY RAND() LIMIT 10';


$result=mysqli_query($db_conx,$sql);


// Associative array
$row = mysqli_fetch_assoc($result);
  
   echo '"' . join('","', $row) . '"';
Link to comment
Share on other sites

Sorry for being such a newbie guys.  Just fixed the single echo with a while loop. But the only problem is that the comma between echos is not work.

 

My result is:  "_IvF4X658hY""DRF6b1N_KFk""G-keFAKQHy4"              

 

My code: 

 

$sql = 'SELECT video_code FROM yt_videos WHERE yt_video_item_id="'.$vidid.'" ORDER BY RAND() LIMIT 10';


$result=mysqli_query($db_conx,$sql);


// Associative array
while ( $row = mysqli_fetch_assoc($result)) 
  
   echo '"' . join('","', $row) . '"'  
Link to comment
Share on other sites

The code works for me when I test it like this, basically simulating an array.

$a = array(
	"_IvF4X658hY",
	"DRF6b1N_KFk",
	"G-keFAKQHy4"
);

 echo '"' . join('","', $a) . '"'

The problem is likely that $row is not what you want to give the join(), meaning it's probably not formatted right.  Do a print_r($row); in the while() and post it back here so I can see what the array looks like.

Link to comment
Share on other sites

I was curious why you are trying to echo them wrapped double quotes and commas

 

 

Make it an array in the loop

$sql = 'SELECT video_code FROM yt_videos WHERE yt_video_item_id="'.$vidid.'" ORDER BY RAND() LIMIT 10';

$result=mysqli_query($db_conx,$sql);
$video_codes = array();
// Associative array
while ( $row = mysqli_fetch_assoc($result)){
    $video_codes[] = $row['video_code'];
    echo $row['video_code']."<br />";
}

if(!empty($video_codes)){
print_r($video_codes);
echo '"' . join('","', $video_codes) . '"';
}


Link to comment
Share on other sites

Store the things you want to join in the array

$sql = 'SELECT video_code FROM yt_videos WHERE yt_video_item_id="'.$vidid.'" ORDER BY RAND() LIMIT 10';

$result=mysqli_query($db_conx,$sql);

$codes = array();

while ($row = mysqli_fetch_assoc($result)) {
    $codes[] = $row['video_code'];  // add code to the array
}   

// now join the array of codes
echo '"' . join('","', $codes) . '"';

Link to comment
Share on other sites

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.