Problema inserimento ciclo foreach o while in variabile

otto9due

Utente Attivo
22 Feb 2014
591
25
28
Mi spiego meglio.. forse non sono riuscito a trovare un titolo valido per spiegare il mio problema..
Ho costruito una classe per l'invio di una newsletter.. Ora il mio problema sta nel importare i prodotti in offerta presenti nel mio db ed inserirli all'interno della mail da inviare agli utenti..

Ecco il codice.. Segno la parte che vorrei si ripetesse ( tramite un ciclo sia esso foreach o while ) tante volte quante sono le offerte.. esattamente come farei dovendo effettuare un select in una pagina..

PHP:
 		protected function TextMess()
		{ 
		
		$this->control = isset($_POST['control']) ? sottrai : aggiungi;
		
		// Estraggo i dati prodotti da db  
		  include '../db/connection.php'; 
		  try
		{
		  $sql = 'SELECT id, titolo, prezzod, prezzop, linkimm, linkprod, percentuale, data, ora, spedizione, click, pub, categoria FROM prodotti ORDER BY data DESC, ora DESC WHERE data = CURDATE() AND pub = 1';
		  $result = $pdo->query($sql);
		}
		catch (PDOException $e)
		{
		  $error = 'Error: ' . $e->getMessage();
		  include '../db/ERRORDB/outputpage.php';
		  exit();
		}     
		foreach ($result as $row)
		{
		  $prodotti[] = array(
			'id' => $row['id'],
			'titolo' => $row['titolo'],
			'prezzod' => $row['prezzod'],
			'prezzop' => $row['prezzop'],
			'linkimm' => $row['linkimm'],
			'linkprod' => $row['linkprod'],
			'percentuale' => $row['percentuale'],
			'data' => $row['data'],
			'ora' => $row['ora'],
			'spedizione' => $row['spedizione'],
			'click' => $row['click'],
			'pub' => $row['pub'],
			'categoria' => $row['categoria']
			
		  );
		}
		// fine estrazione dati prodotti da db
		  
		  if($this->control = "sottrai"){
			$this->textm = $_POST['mess'];
		  }
		  if($this->control = "aggiungi"){
			  
		  	foreach ($prodotti as $prodotto) {
			   	$this->id = $prodotto["id"];
				$this->titolo = $prodotto["titolo"];
				$this->prezzod = $prodotto["prezzod"];
				$this->prezzop = $prodotto["prezzop"];
				$this->linkimm = $prodotto["linkimm"];
				$this->linkprod = $prodotto["linkprod"];
				$this->percentuale = $prodotto["percentuale"];
			}
			$this->textm = "<html>
			<head>
			<style type='text/css'>
			body{
				font-family:'Lucida Grande', Arial;
				color:#333;
				font-size:16px;
			}
			</style>
			</head>
			<body>
			<p>'".$_POST['mess']."'</p>
			<br /> 
			<h2>Nuove offerte inserite su .........mio sito</h2>
			<br />


//----------------------------------INIZIO TABELLA DA RIPETERE------------------------------
			<table width='100%' border='0' cellspacing='0' cellpadding='2'>
			  <tr>
				<td><img src=".$this->linkimm." alt='Immagine prodotto' height='150px' width='180px'></td>
				<td><a href='".$this->linkprod."'>".$this->titolo."</a></td>
			  </tr>
			  <tr>
				<td>Sconto del ".$this->percentuale."</td>
				<td>Il prezzo è sceso da ".$this->prezzop." a ".$this->prezzod.".</td>
			  </tr>
			</table>

//----------------------------------------------FINE TABELLA DA RIPETERE-----------------------
			  

<br>
			  Si tratta di offerte lastminutes quindi affrettati a concludere l'affare!<br>
			  Saluti, lo Staff.</div>  
			  <br />
			  <hr>
			 
			</body>
			</html>
			";
		  }
		  
		  return $this->textm;
		}

In questo esempio ovviamente il foreach non cicla nulla.. ed i dati nella tabella html non si vedrebbero..
 
Ultima modifica:
Forse ho trovato una soluzione.. La posto dovesse interessare a qualcuno:

PHP:
protected function TextMess()
		{ 
$textm = "";
foreach ($prodotti as $prodotto) :

$textm .= " tutto quella che ".$prodotto["titolo"]." va ciclato ";

endforeach;
 
return $textm;
}
 
Ultima modifica:

Discussioni simili